• 这是之前在TesterHome游戏测试群里,jiazurongyu说要搞个手册,然后用了我https://testerhome.com/topics/6956这文章,我感觉那文章不好,就重新写了这篇文章

  • 我没想过要打赏啊╮(╯▽╰)╭

  • 示例声明:1、表示示例中的代码为了便于理解,对于异步的代码个人感觉写得有点不太好,但还是就那样吧;2、无论是List ClientDataList还是List ServerDataList,其第一个元素表示的是一个byte[]类型的协议编号,否则会出错
    示例流程:(对于如何建立服务端与客户端的连接,这里先暂且略过,这儿先讲数据传输)客户端获取List ClientDataList中所含元素的数量ClientDataQuantity,并发送给服务端---->服务端接收ClientDataQuantity,让服务端知道接下来要接收多少个数据---->客户端遍历List ClientDataList中的元素,并循环打包成 前4字节表示数据大小+后面的字节表示数据 的格式,并发送---->服务端根据之前客户端发来的ClientDataQuantity,循环接收客户端发来的数据包,接收单个数据包的方法是先接收4字节的数据获得接下来所需接收的数据大小,然后接收数据---->服务端每接收一个byte[]的数据,便将该数据添加到List ClientDataList---->当服务端获得完整的List ClientDataList后,便解析List ClientDataList中的第一个元素,获得协议编号---->服务端根据协议编号,利用反射的方法,获得所需调用的协议方法,并使用List ClientDataList和MethodInfo.Invoke()的方法获得List ServerDataList---->服务端安装之前客户端发送数据的方式,发送List ServerDataList---->客户端也按照之前服务端接收数据的方式,接收List ServerDataList

    示例语义:其就是List ClientDataList和List ServerDataList中所含的数据及其排序,外加上在发送数据时最开始发送的ClientDataQuantity及ServerDataQuantity
    示例语法:其就是在打包List ClientDataList和List ServerDataList时所采用的格式,前4字节表示数据大小+后面的字节表示数据
    示例时序:短连接,服务端先调用TCP_Start的方法,建立用于监听的Socket ListenSocket,并绑定端口,及开始监听---->客户端创建用于连接的Socket ConnectSocket,并请求连接---->服务端开始接受客户端的连接请求---->服务端与客户端相互传输数据---->服务端调用Disconnect()的方法关闭与该客户端的连接---->客户端调用TCP_End的方法关闭与服务端的连接

  • C#:使用System.Net.Sockets命名空间编写的简单的TCP通讯示例(包含同步服务端、同步客户端、异步服务端、异步客户端)

    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Sockets;
    using System.Reflection;
    using System.Text;
    using System.Threading;
    
    namespace SinDynasty
    {
        public class SyncServer
        {
            private class SyncTCP
            {
                private static Socket ListenSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                private Socket ConnectSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                private List<byte[]> ClientDataList = new List<byte[]>();
                private List<byte[]> ServerDataList = new List<byte[]>();
    
                public static void Listen(string ServerIP, int ServerPort, int MaxListenNumber)
                {
                    IPAddress ServerIPAddress = IPAddress.Parse(ServerIP);
                    IPEndPoint ServerIPEndPoint = new IPEndPoint(ServerIPAddress, ServerPort);
                    ListenSocket.Bind(ServerIPEndPoint);
                    ListenSocket.Listen(MaxListenNumber);
                }//void Listen(string ServerIP, int ServerPort, int MaxListenNumber)
    
                public void Accept()
                {
                    ConnectSocket = ListenSocket.Accept();
                }//void Accept()
    
                public void Receive()
                {
                    byte[] ClientDataQuantity = new byte[4];
                    ConnectSocket.Receive(ClientDataQuantity, 0, ClientDataQuantity.Length, SocketFlags.None);
    
                    long CycleIndex = 0;
                    while (CycleIndex < BitConverter.ToInt32(ClientDataQuantity, 0))
                    {
                        byte[] ClientDataLength = new byte[4];
                        ConnectSocket.Receive(ClientDataLength, 0, ClientDataLength.Length, SocketFlags.None);
    
                        byte[] ClientData = new byte[0];
                        if (BitConverter.ToInt32(ClientDataLength, 0) > 0)
                        {
                            ClientData = new byte[BitConverter.ToInt32(ClientDataLength, 0)];
                            ConnectSocket.Receive(ClientData, 0, ClientData.Length, SocketFlags.None);
                        }
    
                        ClientDataList.Add(ClientData);
    
                        CycleIndex = CycleIndex + 1;
                    }
                }//void Receive()
    
                public void ProtocolHandler()
                {
                    byte[] ProtocolNumber = ClientDataList[0];
                    if (ProtocolNumber.Length != 4)
                    {
                        ProtocolNumber = BitConverter.GetBytes(0);
                    }
                    string ProtocolName = "Protocol_" + BitConverter.ToInt32(ProtocolNumber, 0);
                    Protocol P = new Protocol();
                    Type T = P.GetType();
                    MethodInfo ProtocolMethodInfo = T.GetMethod(ProtocolName);
                    if (ProtocolMethodInfo == null)
                    {
                        ProtocolMethodInfo = T.GetMethod("Protocol_0");
                    }
                    object[] ProtocolParameter = { ClientDataList };
                    ServerDataList = (List<byte[]>)ProtocolMethodInfo.Invoke(null, ProtocolParameter);
                }//void ProtocolHandler()
    
                public void Send()
                {
                    byte[] ServerDataQuantity = BitConverter.GetBytes(ServerDataList.Count);
                    ConnectSocket.Send(ServerDataQuantity, 0, ServerDataQuantity.Length, SocketFlags.None);
    
                    foreach (byte[] ServerData in ServerDataList)
                    {
                        byte[] ServerDataLength = BitConverter.GetBytes(ServerData.Length);
                        ConnectSocket.Send(ServerDataLength, 0, ServerDataLength.Length, SocketFlags.None);
    
                        ConnectSocket.Send(ServerData, 0, ServerData.Length, SocketFlags.None);
                    }
                }//void Send()
    
                public void Disconnect()
                {
                    if (ConnectSocket != null)
                    {
                        ConnectSocket.Disconnect(true);
                    }
                }//void DisconnectConnect()
    
                private class Protocol
                {
                    public static List<byte[]> Protocol_0(List<byte[]> ClientDataList)
                    {
                        List<byte[]> ServerDataList = new List<byte[]>();
                        ServerDataList.Add(Encoding.Default.GetBytes("未知的协议"));
    
                        return ServerDataList;
                    }//List<byte[]> Protocol_0(List<byte[]> ClientDataList)
    
                    public static List<byte[]> Protocol_1(List<byte[]> ClientDataList)
                    {
                        List<byte[]> ServerDataList = ClientDataList;
    
                        return ServerDataList;
                    }//List<byte[]> Protocol_1(List<byte[]> ClientDataList)
    
                }//class Protocol
    
            }//class SyncTCP
    
            public static void TCP_Start(string ServerIP, int ServerPort, int MaxListenNumber)
            {
                SyncTCP.Listen(ServerIP, ServerPort, MaxListenNumber);
            }//void TCP_Start(string ServerIP, int ServerPort, int MaxListenNumber)
    
            public static void TCP()
            {
                SyncTCP Server = new SyncTCP();
                while (true)
                {
                    Server.Accept();
                    Server.Receive();
                    Server.ProtocolHandler();
                    Server.Send();
                    Server.Disconnect();
                }
            }//void TCP()
    
        }//class SyncServer
    
        public class SyncClient
        {
            private class SyncTCP
            {
                private static Socket ConnectSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                public List<byte[]> ClientDataList = new List<byte[]>();
                public List<byte[]> ServerDataList = new List<byte[]>();
    
                public static void Connect(string ServerIP, int ServerPort)
                {
                    IPAddress ServerIPAddress = IPAddress.Parse(ServerIP);
                    IPEndPoint ServerIPEndPoint = new IPEndPoint(ServerIPAddress, ServerPort);
                    ConnectSocket.Connect(ServerIPEndPoint);
                }//void Connect(string ServerIP, int ServerPort)
    
                public void Send()
                {
                    byte[] ClientDataQuantity = BitConverter.GetBytes(ClientDataList.Count);
                    ConnectSocket.Send(ClientDataQuantity, 0, ClientDataQuantity.Length, SocketFlags.None);
    
                    foreach (byte[] ClientData in ClientDataList)
                    {
                        byte[] ClientDataLength = BitConverter.GetBytes(ClientData.Length);
                        ConnectSocket.Send(ClientDataLength, 0, ClientDataLength.Length, SocketFlags.None);
    
                        ConnectSocket.Send(ClientData, 0, ClientData.Length, SocketFlags.None);
                    }
                }//void Send()
    
                public void Receive()
                {
                    byte[] ServerDataQuantity = new byte[4];
                    ConnectSocket.Receive(ServerDataQuantity, 0, ServerDataQuantity.Length, SocketFlags.None);
    
                    long CycleIndex = 0;
                    while (CycleIndex < BitConverter.ToInt32(ServerDataQuantity, 0))
                    {
                        byte[] ServerDataLength = new byte[4];
                        ConnectSocket.Receive(ServerDataLength, 0, ServerDataLength.Length, SocketFlags.None);
    
                        byte[] ServerData = new byte[0];
                        if (BitConverter.ToInt32(ServerDataLength, 0) > 0)
                        {
                            ServerData = new byte[BitConverter.ToInt32(ServerDataLength, 0)];
                            ConnectSocket.Receive(ServerData, 0, ServerData.Length, SocketFlags.None);
                        }
    
                        ServerDataList.Add(ServerData);
    
                        CycleIndex = CycleIndex + 1;
                    }
                }//void Receive()
    
                public static void Disconnect()
                {
                    if (ConnectSocket != null)
                    {
                        ConnectSocket.Disconnect(true);
                    }
                }//void Disconnect()
    
            }//class SyncTCP
    
            public static void TCP_Start(string ServerIP, int ServerPort)
            {
                SyncTCP.Connect(ServerIP, ServerPort);
            }//void TCP_Start(string ServerIP, int ServerPort)
    
            public static List<byte[]> TCP(List<byte[]> ClientDataList)
            {
                SyncTCP TCP = new SyncTCP();
                TCP.ClientDataList = ClientDataList;
                TCP.Send();
                TCP.Receive();
                List<byte[]> ServerDataList = TCP.ServerDataList;
    
                return ServerDataList;
            }//List<byte[]> TCP(List<byte[]> ClientDataList)
    
            public static void TCP_End()
            {
                SyncTCP.Disconnect();
            }//void TCP_End()
    
        }//class SyncClient
    
        public class AsyncServer
        {
            private class AsyncTCP
            {
                private static Socket ListenSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                private Socket ConnectSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                private List<byte[]> ClientDataList = new List<byte[]>();
                private List<byte[]> ServerDataList = new List<byte[]>();
    
                public static void Listen(string ServerIP, int ServerPort, int MaxListenNumber)
                {
                    IPAddress ServerIPAddress = IPAddress.Parse(ServerIP);
                    IPEndPoint ServerIPEndPoint = new IPEndPoint(ServerIPAddress, ServerPort);
                    ListenSocket.Bind(ServerIPEndPoint);
                    ListenSocket.Listen(MaxListenNumber);
                }//void Listen(string ServerIP, int ServerPort, int MaxListenNumber)
    
                private ManualResetEvent MRE_Accept = new ManualResetEvent(false);
                public void Accept()
                {
                    MRE_Accept.Reset();
    
                    ListenSocket.BeginAccept(AcceptCallBack, null);
    
                    MRE_Accept.WaitOne();
                }//void Accept()
    
                private void AcceptCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket = ListenSocket.EndAccept(AsyncResult);
    
                    MRE_Accept.Set();
                }//void AcceptCallBack(IAsyncResult AsyncResult)
    
                private ManualResetEvent MRE_Receive = new ManualResetEvent(false);
                private ManualResetEvent MRE_ReceiveClientDataQuantity = new ManualResetEvent(false);
                private ManualResetEvent MRE_ReceiveClientDataLength = new ManualResetEvent(false);
                private ManualResetEvent MRE_ReceiveClientData = new ManualResetEvent(false);
    
                public void Receive()
                {
                    MRE_Receive.Reset();
    
                    MRE_ReceiveClientDataQuantity.Reset();
    
                    byte[] ClientDataQuantity = new byte[4];
                    object[] ReceiveClientDataQuantityState = { ClientDataQuantity };
                    ConnectSocket.BeginReceive(ClientDataQuantity, 0, ClientDataQuantity.Length, SocketFlags.None, ReceiveClientDataQuantityCallBack, ReceiveClientDataQuantityState);
    
                    MRE_ReceiveClientDataQuantity.WaitOne();
    
                    MRE_Receive.WaitOne();
                }//void Receive()
    
                private void ReceiveClientDataQuantityCallBack(IAsyncResult AsyncResult)
                {
                    object[] ReceiveClientDataQuantityState = (object[])AsyncResult.AsyncState;
                    byte[] ClientDataQuantity = (byte[])ReceiveClientDataQuantityState[0];
                    ConnectSocket.EndReceive(AsyncResult);
    
                    MRE_ReceiveClientDataQuantity.Set();
    
                    long CycleIndex = 0;
                    while (CycleIndex < BitConverter.ToInt32(ClientDataQuantity, 0))
                    {
                        MRE_ReceiveClientDataLength.Reset();
    
                        byte[] ClientDataLength = new byte[4];
                        ConnectSocket.BeginReceive(ClientDataLength, 0, ClientDataLength.Length, SocketFlags.None, ReceiveClientDataLengthCallBack, null);
    
                        MRE_ReceiveClientDataLength.WaitOne();
    
                        byte[] ClientData = new byte[0];
                        if (BitConverter.ToInt32(ClientDataLength, 0) > 0)
                        {
                            MRE_ReceiveClientData.Reset();
    
                            ClientData = new byte[BitConverter.ToInt32(ClientDataLength, 0)];
                            ConnectSocket.BeginReceive(ClientData, 0, ClientData.Length, SocketFlags.None, ReceiveClientDataCallBack, null);
    
                            MRE_ReceiveClientData.WaitOne();
                        }
    
                        ClientDataList.Add(ClientData);
    
                        CycleIndex = CycleIndex + 1;
                    }
    
                    MRE_Receive.Set();
                }//void ReceiveClientDataQuantityCallBack(IAsyncResult AsyncResult)
    
                private void ReceiveClientDataLengthCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndReceive(AsyncResult);
    
                    MRE_ReceiveClientDataLength.Set();
                }//void ReceiveClientDataLengthCallBack(IAsyncResult AsyncResult)
    
                private void ReceiveClientDataCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndReceive(AsyncResult);
    
                    MRE_ReceiveClientData.Set();
                }//void ReceiveClientDataCallBack(IAsyncResult AsyncResult)
    
                public void ProtocolHandler()
                {
                    byte[] ProtocolNumber = ClientDataList[0];
                    if (ProtocolNumber.Length != 4)
                    {
                        ProtocolNumber = BitConverter.GetBytes(0);
                    }
                    string ProtocolName = "Protocol_" + BitConverter.ToInt32(ProtocolNumber, 0);
                    Protocol P = new Protocol();
                    Type T = P.GetType();
                    MethodInfo ProtocolMethodInfo = T.GetMethod(ProtocolName);
                    if (ProtocolMethodInfo == null)
                    {
                        ProtocolMethodInfo = T.GetMethod("Protocol_0");
                    }
                    object[] ProtocolParameter = { ClientDataList };
                    ServerDataList = (List<byte[]>)ProtocolMethodInfo.Invoke(null, ProtocolParameter);
                }//void ProtocolHandler()
    
                private ManualResetEvent MRE_Send = new ManualResetEvent(false);
                private ManualResetEvent MRE_SendServerDataQuantity = new ManualResetEvent(false);
                private ManualResetEvent MRE_SendServerDataLength = new ManualResetEvent(false);
                private ManualResetEvent MRE_SendServerData = new ManualResetEvent(false);
                public void Send()
                {
                    MRE_Send.Reset();
    
                    MRE_SendServerDataQuantity.Reset();
    
                    byte[] ServerDataQuantity = BitConverter.GetBytes(ServerDataList.Count);
                    ConnectSocket.BeginSend(ServerDataQuantity, 0, ServerDataQuantity.Length, SocketFlags.None, SendServerDataQuantityCallBack, null);
    
                    MRE_SendServerDataQuantity.WaitOne();
    
                    MRE_Send.WaitOne();
                }//void Send()
    
                private void SendServerDataQuantityCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndSend(AsyncResult);
    
                    MRE_SendServerDataQuantity.Set();
    
                    foreach (byte[] ServerData in ServerDataList)
                    {
                        MRE_SendServerDataLength.Reset();
    
                        byte[] ServerDataLength = BitConverter.GetBytes(ServerData.Length);
                        ConnectSocket.BeginSend(ServerDataLength, 0, ServerDataLength.Length, SocketFlags.None, SendServerDataLengthCallBack, null);
    
                        MRE_SendServerDataLength.WaitOne();
    
                        MRE_SendServerData.Reset();
    
                        ConnectSocket.BeginSend(ServerData, 0, ServerData.Length, SocketFlags.None, SendServerDataCallBack, null);
    
                        MRE_SendServerData.WaitOne();
                    }
    
                    MRE_Send.Set();
                }//void SendServerDataQuantityCallBack(IAsyncResult AsyncResult)
    
                private void SendServerDataLengthCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndSend(AsyncResult);
    
                    MRE_SendServerDataLength.Set();
                }//void SendServerDataLengthCallBack(IAsyncResult AsyncResult)
    
                private void SendServerDataCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndSend(AsyncResult);
    
                    MRE_SendServerData.Set();
                }//void SendServerDataCallBack(IAsyncResult AsyncResult)
    
                private ManualResetEvent MRE_Disconnect = new ManualResetEvent(false);
                public void Disconnect()
                {
                    if (ConnectSocket != null)
                    {
                        MRE_Disconnect.Reset();
    
                        ConnectSocket.BeginDisconnect(true, DisconnectCallBack, null);
    
                        MRE_Disconnect.WaitOne();
                    }
                }//void DisconnectConnect()
    
                private void DisconnectCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndDisconnect(AsyncResult);
    
                    MRE_Disconnect.Set();
                }//void DisconnectCallBack(IAsyncResult AsyncResult)
    
                private class Protocol
                {
                    public static List<byte[]> Protocol_0(List<byte[]> ClientDataList)
                    {
                        List<byte[]> ServerDataList = new List<byte[]>();
                        ServerDataList.Add(Encoding.Default.GetBytes("未知的协议"));
    
                        return ServerDataList;
                    }//List<byte[]> Protocol_0(List<byte[]> ClientDataList)
    
                    public static List<byte[]> Protocol_1(List<byte[]> ClientDataList)
                    {
                        List<byte[]> ServerDataList = ClientDataList;
    
                        return ServerDataList;
                    }//List<byte[]> Protocol_1(List<byte[]> ClientDataList)
    
                }//class Protocol
    
            }//class AsyncTCP
    
            public static void TCP_Start(string ServerIP, int ServerPort, int MaxListenNumber)
            {
                AsyncTCP.Listen(ServerIP, ServerPort, MaxListenNumber);
            }//void TCP_Start(string ServerIP, int ServerPort, int MaxListenNumber)
    
            public static void TCP()
            {
                AsyncTCP Server = new AsyncTCP();
                while (true)
                {
                    Server.Accept();
                    Server.Receive();
                    Server.ProtocolHandler();
                    Server.Send();
                    Server.Disconnect();
                }
            }//void TCP()
    
        }//class AsyncServer
    
        public class AsyncClient
        {
            private class AsyncTCP
            {
                private static Socket ConnectSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                public List<byte[]> ClientDataList = new List<byte[]>();
                public List<byte[]> ServerDataList = new List<byte[]>();
    
                private static ManualResetEvent MRE_Connect = new ManualResetEvent(false);
                public static void Connect(string ServerIP, int ServerPort)
                {
                    MRE_Connect.Reset();
    
                    IPAddress ServerIPAddress = IPAddress.Parse(ServerIP);
                    IPEndPoint ServerIPEndPoint = new IPEndPoint(ServerIPAddress, ServerPort);
                    ConnectSocket.BeginConnect(ServerIPEndPoint, ConnectCallBack, null);
    
                    MRE_Connect.WaitOne();
                }//Socket Connect(string ServerIP, int ServerPort)
    
                private static void ConnectCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndConnect(AsyncResult);
    
                    MRE_Connect.Set();
                }//void ConnectCallBack(IAsyncResult AsyncResult)
    
                private ManualResetEvent MRE_Send = new ManualResetEvent(false);
                private ManualResetEvent MRE_SendClientDataQuantity = new ManualResetEvent(false);
                private ManualResetEvent MRE_SendClientDataLength = new ManualResetEvent(false);
                private ManualResetEvent MRE_SendClientData = new ManualResetEvent(false);
                public void Send()
                {
                    MRE_Send.Reset();
    
                    MRE_SendClientDataQuantity.Reset();
    
                    byte[] ClientDataQuantity = BitConverter.GetBytes(ClientDataList.Count);
                    ConnectSocket.BeginSend(ClientDataQuantity, 0, ClientDataQuantity.Length, SocketFlags.None, SendClientDataQuantityCallBack, null);
    
                    MRE_SendClientDataQuantity.WaitOne();
    
                    MRE_Send.WaitOne();
                }//void Send()
    
                private void SendClientDataQuantityCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndSend(AsyncResult);
    
                    MRE_SendClientDataQuantity.Set();
    
                    foreach (byte[] ClientData in ClientDataList)
                    {
                        MRE_SendClientDataLength.Reset();
    
                        byte[] ClientDataLength = BitConverter.GetBytes(ClientData.Length);
                        ConnectSocket.BeginSend(ClientDataLength, 0, ClientDataLength.Length, SocketFlags.None, SendClientDataLengthCallBack, null);
    
                        MRE_SendClientDataLength.WaitOne();
    
                        MRE_SendClientData.Reset();
    
                        ConnectSocket.BeginSend(ClientData, 0, ClientData.Length, SocketFlags.None, SendClientDataCallBack, null);
    
                        MRE_SendClientData.WaitOne();
                    }
    
                    MRE_Send.Set();
                }//void SendClientDataQuantityCallBack(IAsyncResult AsyncResult)
    
                private void SendClientDataLengthCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndSend(AsyncResult);
    
                    MRE_SendClientDataLength.Set();
                }//void SendClientDataLengthCallBack(IAsyncResult AsyncResult)
    
                private void SendClientDataCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndSend(AsyncResult);
    
                    MRE_SendClientData.Set();
                }//void SendClientDataCallBack(IAsyncResult AsyncResult)
    
                private ManualResetEvent MRE_Receive = new ManualResetEvent(false);
                private ManualResetEvent MRE_ReceiveServerDataQuantity = new ManualResetEvent(false);
                private ManualResetEvent MRE_ReceiveServerDataLength = new ManualResetEvent(false);
                private ManualResetEvent MRE_ReceiveServerData = new ManualResetEvent(false);
                public void Receive()
                {
                    MRE_Receive.Reset();
    
                    MRE_ReceiveServerDataQuantity.Reset();
    
                    byte[] ServerDataQuantity = new byte[4];
                    object[] ReceiveServerDataQuantityState = { ServerDataQuantity };
                    ConnectSocket.BeginReceive(ServerDataQuantity, 0, ServerDataQuantity.Length, SocketFlags.None, ReceiveServerDataQuantityCallBack, ReceiveServerDataQuantityState);
    
                    MRE_ReceiveServerDataQuantity.WaitOne();
    
                    MRE_Receive.WaitOne();
                }//void Receive()
    
                private void ReceiveServerDataQuantityCallBack(IAsyncResult AsyncResult)
                {
                    object[] ReceiveServerDataQuantityState = (object[])AsyncResult.AsyncState;
                    byte[] ServerDataQuantity = (byte[])ReceiveServerDataQuantityState[0];
                    ConnectSocket.EndReceive(AsyncResult);
    
                    MRE_ReceiveServerDataQuantity.Set();
    
                    long CycleIndex = 0;
                    while (CycleIndex < BitConverter.ToInt32(ServerDataQuantity, 0))
                    {
                        MRE_ReceiveServerDataLength.Reset();
    
                        byte[] ServerDataLength = new byte[4];
                        ConnectSocket.BeginReceive(ServerDataLength, 0, ServerDataLength.Length, SocketFlags.None, ReceiveServerDataLengthCallBack, null);
    
                        MRE_ReceiveServerDataLength.WaitOne();
    
                        byte[] ServerData = new byte[0];
                        if (BitConverter.ToInt32(ServerDataLength, 0) > 0)
                        {
                            MRE_ReceiveServerData.Reset();
    
                            ServerData = new byte[BitConverter.ToInt32(ServerDataLength, 0)];
                            ConnectSocket.BeginReceive(ServerData, 0, ServerData.Length, SocketFlags.None, ReceiveServerDataCallBack, null);
    
                            MRE_ReceiveServerData.WaitOne();
                        }
    
                        ServerDataList.Add(ServerData);
    
                        CycleIndex = CycleIndex + 1;
                    }
    
                    MRE_Receive.Set();
                }//void ReceiveClientDataQuantityCallBack(IAsyncResult AsyncResult)
    
                private void ReceiveServerDataLengthCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndReceive(AsyncResult);
    
                    MRE_ReceiveServerDataLength.Set();
                }//void ReceiveClientDataLengthCallBack(IAsyncResult AsyncResult)
    
                private void ReceiveServerDataCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndReceive(AsyncResult);
    
                    MRE_ReceiveServerData.Set();
                }//void ReceiveClientDataCallBack(IAsyncResult AsyncResult)
    
                private static ManualResetEvent MRE_Disconnect = new ManualResetEvent(false);
                public static void Disconnect()
                {
                    if (ConnectSocket != null)
                    {
                        MRE_Disconnect.Reset();
    
                        ConnectSocket.BeginDisconnect(true, DisconnectCallBack, null);
    
                        MRE_Disconnect.WaitOne();
                    }
                }//void DisconnectConnect()
    
                private static void DisconnectCallBack(IAsyncResult AsyncResult)
                {
                    ConnectSocket.EndDisconnect(AsyncResult);
    
                    MRE_Disconnect.Set();
                }//void DisconnectCallBack(IAsyncResult AsyncResult)
    
            }//class AsyncTCP
    
            public static void TCP_Start(string ServerIP, int ServerPort)
            {
                AsyncTCP.Connect(ServerIP, ServerPort);
            }//void TCP_Start(string ServerIP, int ServerPort)
    
            public static List<byte[]> TCP(List<byte[]> ClientDataList)
            {
                AsyncTCP TCP = new AsyncTCP();
                TCP.ClientDataList = ClientDataList;
                TCP.Send();
                TCP.Receive();
                List<byte[]> ServerDataList = TCP.ServerDataList;
    
                return ServerDataList;
            }//List<byte[]> TCP(List<byte[]> ClientDataList)
    
            public static void TCP_End()
            {
                AsyncTCP.Disconnect();
            }//void TCP_End()
    
        }//class AsyncClient
    
    }//namespace SinDynasty
    
  • 表示里面居然有我的文章,感觉写得好羞耻

  • #9楼 @Lihuazhang 恩,是微软的UIAutomation

  • #4楼 @jiazurongyu 我还有个异步的没写,之前被我忘记了,我准备这周有空就写下

  • ╮(╯▽╰)╭

  • #1楼 @hu_qingen 我写这个主要是总结一下我最近的研究