Ejemplo n.º 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 = "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());
                }
            }
        }
Ejemplo n.º 2
0
        static void Run(string[] args)
        {
            var performanceCountersCategoryName = "Microshaoft EasyPerformanceCounters Category";
            var enableCounters = MultiPerformanceCountersTypeFlags.ProcessCounter
                                 | MultiPerformanceCountersTypeFlags.ProcessedAverageTimerCounter
                                 | MultiPerformanceCountersTypeFlags.ProcessedCounter
                                 | MultiPerformanceCountersTypeFlags.ProcessedRateOfCountsPerSecondCounter
                                 | MultiPerformanceCountersTypeFlags.ProcessingCounter;
            var sendEncoding    = Encoding.Default;
            var receiveEncoding = Encoding.Default;

            //byte[] data = new byte[1024];
            string[]   a     = args[0].Split(new char[] { ':' });
            string     ip    = a[0];
            int        port  = int.Parse(a[1]);
            IPEndPoint ipep1 = new IPEndPoint(IPAddress.Parse(ip), port);

            Console.WriteLine("ipep1 {0}", ipep1.ToString());
            a    = args[1].Split(new char[] { ':' });
            ip   = a[0];
            port = int.Parse(a[1]);
            IPEndPoint ipep2 = new IPEndPoint(IPAddress.Parse(ip), port);

            Console.WriteLine("ipep2 {0}", ipep2.ToString());
            var    remoteAnyIPEP = new IPEndPoint(IPAddress.Any, 0);
            Socket socket1       = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket1.Bind(ipep1);
            SocketAsyncDataHandler <string> handler1 = new SocketAsyncDataHandler <string>(socket1, 1);
            var receiveSocketAsyncEventArgs1         = _socketAsyncEventArgsPool.Pop();

            _bufferManager.SetBuffer(receiveSocketAsyncEventArgs1);
            handler1.StartReceiveDataFrom
            (
                remoteAnyIPEP
                , receiveSocketAsyncEventArgs1
                , (x, y, z, w) =>
            {
                Console.WriteLine("次数: {0}", x.ReceivedCount);
                Console.WriteLine("字节: {0}", z.Length);
                EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                .CountPerformance
                (
                    enableCounters
                    , performanceCountersCategoryName
                    , "Hander1::Received"
                    , null
                    , () =>
                {
                    var ss = receiveEncoding.GetString(z);
                    //Console.Write(s);
                    Console.WriteLine
                    (
                        "from {0} , to {1}, data {2}"
                        , x.WorkingSocket.LocalEndPoint
                        , w.RemoteEndPoint
                        , ss
                    );
                }
                    , null
                    , null
                    , null
                );
                return(false);
            }
                , (x, y, z) =>
            {
                Console.WriteLine(z.ToString());
                return(true);
            }
            );
            Socket socket2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket2.Bind(ipep2);
            SocketAsyncDataHandler <string> handler2 = new SocketAsyncDataHandler <string>(socket2, 2);
            var receiveSocketAsyncEventArgs2         = _socketAsyncEventArgsPool.Pop();

            _bufferManager.SetBuffer(receiveSocketAsyncEventArgs2);
            handler2.StartReceiveDataFrom
            (
                remoteAnyIPEP
                , receiveSocketAsyncEventArgs2
                , (x, y, z, w) =>
            {
                Console.WriteLine("次数: {0}", x.ReceivedCount);
                Console.WriteLine("字节: {0}", z.Length);
                EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                .CountPerformance
                (
                    enableCounters
                    , performanceCountersCategoryName
                    , "Hander2::Received"
                    , null
                    , () =>
                {
                    var ss = receiveEncoding.GetString(z);
                    //Console.Write(s);
                    Console.WriteLine
                    (
                        "from {0} , to {1}, data {2}"
                        , x.WorkingSocket.LocalEndPoint
                        , w.RemoteEndPoint
                        , ss
                    );
                }
                    , null
                    , null
                    , null
                );
                return(false);
            }
                , (x, y, z) =>
            {
                Console.WriteLine(z.ToString());
                return(true);
            }
            );
            string s = string.Empty;

            Console.WriteLine("Send ...");
            while ((s = Console.ReadLine().ToLower()) != "q")
            {
                var buffer = sendEncoding.GetBytes(s);
                Parallel.For
                (
                    0
                    , 1000
                    , new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 1 // Environment.ProcessorCount
                                               //, TaskScheduler = null
                }
                    , i =>
                {
                    Thread.Sleep(5);
                    EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                    .CountPerformance
                    (
                        enableCounters
                        , performanceCountersCategoryName
                        , "Hander1::Sended"
                        , null
                        , () =>
                    {
                        handler1.SendDataToSync(buffer, ipep2);
                    }
                        , null
                        , null
                        , null
                    );
                    EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                    .CountPerformance
                    (
                        enableCounters
                        , performanceCountersCategoryName
                        , "Hander2::Sended"
                        , null
                        , () =>
                    {
                        handler2.SendDataToSync(buffer, ipep1);
                    }
                        , null
                        , null
                        , null
                    );
                }
                );
            }
            var e = handler1.ReceiveSocketAsyncEventArgs;

            //_bufferManager.FreeBuffer(e);
            _socketAsyncEventArgsPool.Push(e);
            e = handler2.ReceiveSocketAsyncEventArgs;
            //_bufferManager.FreeBuffer(e);
            _socketAsyncEventArgsPool.Push(e);
            handler1.DestoryWorkingSocket();
            handler2.DestoryWorkingSocket();
            Console.WriteLine("Send quit");
        }