Exemple #1
0
        static void Main(string[] args)
        {
#if NETCOREAPP2_0
            Console.WriteLine(RuntimeInformation.OSArchitecture.ToString());
            Console.WriteLine(RuntimeInformation.OSDescription);
            Console.WriteLine(RuntimeInformation.FrameworkDescription);
#endif
            Console.Title = "Server";

            var       receiveEncoding = Encoding.UTF8;
            var       sendEncoding    = Encoding.UTF8;
            var       port            = 18180;
            IPAddress ipa;
            IPAddress.TryParse("127.0.0.1", out ipa);
            ipa = IPAddress.Any;

            var es = new EchoServer <string>
                     (
                new IPEndPoint(ipa, port)
                , (x, y) =>
            {
                var s = receiveEncoding.GetString(y);
                s     = string
                        .Format
                        (
                    "Echo: {0}{1}{0}{2}{0}{3}{0}"
                    , "\r\n"
                    , s
                    , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
#if NETCOREAPP2_0
                    , RuntimeInformation.OSDescription
#else
                    , ""
#endif
                        );
                Console.WriteLine(s);
                Console.WriteLine($"Server ReceivedAsyncCount: {x.ReceivedAsyncCount}");
                Console.WriteLine($"Server ReceivedSyncCount: {x.ReceivedSyncCount}");
                Console.WriteLine($"Server ReceivedCount: {x.ReceivedCount}");

                Console.WriteLine($"Server ReceivedHeadersCount: {x.ReceivedHeadersCount}");
                Console.WriteLine($"Server ReceivedBodysCount: {x.ReceivedBodysCount}");

                Console.WriteLine($"Server ReceivedTotalBytesCount: {x.ReceivedTotalBytesCount} bytes");

                var buffer      = sendEncoding.GetBytes(s);
                byte[] intBytes = BytesHelper.GetLengthHeaderBytes(buffer);
                x.SendDataSync(intBytes);
                x.SendDataSync(buffer);
            }
                     );
            Console.WriteLine("Hello World");
            Console.WriteLine(Environment.Version.ToString());
            Console.ReadLine();
        }
Exemple #2
0
        static void Main(string[] args)
        {
#if NETCOREAPP2_0
            Console.WriteLine(RuntimeInformation.OSArchitecture.ToString());
            Console.WriteLine(RuntimeInformation.OSDescription);
            Console.WriteLine(RuntimeInformation.FrameworkDescription);
#endif

            Console.Title = "Client";
            int port = 18180;
            Console.WriteLine("Please input port to connect ...");
            if
            (
                int.TryParse
                (
                    Console.ReadLine()
                    , out int p
                )
            )
            {
                port = p;
            }
            Console.WriteLine($"Connect Port: {port}");
            IPAddress ipa;
            IPAddress.TryParse("127.0.0.1", out ipa);
            var socket = new Socket
                         (
                AddressFamily.InterNetwork
                , SocketType.Stream
                , ProtocolType.Tcp
                         );
            var ipep = new IPEndPoint(ipa, port);
            socket.Connect(ipep);

            //Console.ReadLine();
            var handler = new SocketAsyncDataHandler <string>
                          (
                socket
                , 1
                          );
            var sendEncoding    = Encoding.UTF8;
            var receiveEncoding = Encoding.UTF8;

            handler
            .StartReceiveWholeDataPackets
            (

                4
                , 0
                , 4
                , () =>
            {
                var saea = new SocketAsyncEventArgs();
                saea.SetBuffer
                (
                    new byte[64 * 1024]
                    , 0
                    , 64 * 1024
                );
                return(saea);
            }
                , (x, y, z) =>
            {
                var s = receiveEncoding.GetString(y);
                //Console.WriteLine("SocketID: {1}{0}Length: {2}{0}Data: {2}", "\r\n", x.SocketID, y.Length ,s);
                Console.Write(s);
                return(true);
            }
            );
            string input = string.Empty;
            while ((input = Console.ReadLine()) != "q")
            {
                try
                {
                    var    buffer   = sendEncoding.GetBytes(input);
                    var    l        = buffer.Length;
                    byte[] intBytes = BytesHelper.GetLengthHeaderBytes(buffer);
                    handler.SendDataSync(intBytes);
                    handler.SendDataSync(buffer);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }