1、UDP
public class AsyncSocketUdp { private Socket socket; private IPEndPoint localEP; private IPEndPoint remoteEP; private byte[] buffer = new byte[1024];//用于存放接收消息 public event Action<byte[]> OnDataReceive; /// <summary> /// 初始化 /// </summary> /// <param name="localIP"></param> /// <param name="localPort"></param> public void Init(IPAddress localIP, int localPort) { //创建一个Socket实例 socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //关联本地终结点 localEP = new IPEndPoint(localIP, localPort); socket.Bind(localEP); } /// <summary> /// 初始化 /// </summary> public void Init(IPAddress localIP, int localPort, IPAddress remoteIp, int remotePort) { //创建一个Socket实例 socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //关联本地终结点 localEP = new IPEndPoint(localIP, localPort); remoteEP = new IPEndPoint(remoteIp, remotePort); socket.Bind(localEP); } /// <summary> /// 接收消息 /// </summary> public void ReceiveData() { EndPoint ep = (EndPoint)remoteEP; this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref ep, new AsyncCallback(ReceiveCallBack), null); } /// <summary> /// 接收消息 /// </summary> /// <param name="remoteIp"></param> /// <param name="remotePort"></param> public void ReceiveData(IPAddress remoteIp,int remotePort) { remoteEP = new IPEndPoint(remoteIp, remotePort); EndPoint ep=(EndPoint)remoteEP; this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref ep, new AsyncCallback(ReceiveCallBack), null); } /// <summary> /// 接收消息的回调 /// </summary> /// <param name="ar"></param> private void ReceiveCallBack(IAsyncResult ar) { int length= socket.EndReceive(ar);//结束挂起的异步读取 byte[] data = new byte[length]; Array.Copy(buffer, 0, data, 0, data.Length); OnDataReceive(data);//接收的数据 EndPoint ep = (EndPoint)remoteEP; this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref ep, new AsyncCallback(ReceiveCallBack), null); } /// <summary> /// 异步发送消息 /// </summary> /// <param name="data"></param> public void SendData(byte[] data,IPEndPoint remoteEP) { if (socket != null) { socket.BeginSendTo(data, 0, data.Length, SocketFlags.None, remoteEP, new AsyncCallback(SendCallBack), null); } } /// <summary> /// 发送数据的回调 /// </summary> /// <param name="ar"></param> private void SendCallBack(IAsyncResult ar) { int length = socket.EndSend(ar);//结束挂起的异步发送 } /// <summary> /// 关闭 /// </summary> public void Close() { if (socket != null) { socket.Close(); } } }
2、TCP
public class AsyncSocketTcp { private Socket socket; private IPEndPoint localEP; private IPEndPoint remoteEP; private byte[] buffer = new byte[1024];//用于存放接收消息 public event Action<byte[]> OnDataReceive; /// <summary> /// tcp连接 /// </summary> /// <param name="remoteIP"></param> /// <param name="remotePort"></param> public void Connet(IPAddress remoteIP,int remotePort) { //创建一个Socket实例 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //建立连接 remoteEP=new IPEndPoint(remoteIP, remotePort); socket.BeginConnect(remoteEP, new AsyncCallback(ConnectCallBack), this.socket); } /// <summary> /// tcp连接 /// </summary> public void Connet(IPAddress localIP, int localPort,IPAddress remoteIP, int remotePort) { //创建一个Socket实例 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //建立连接 localEP = new IPEndPoint(localIP, localPort); remoteEP = new IPEndPoint(remoteIP, remotePort); socket.Bind(localEP);//绑定本地终结点 socket.BeginConnect(remoteEP, new AsyncCallback(ConnectCallBack), this.socket); } /// <summary> /// 连接请求的回调 /// </summary> /// <param name="ar"></param> private void ConnectCallBack(IAsyncResult ar) { Socket socketHandle =(Socket)ar.AsyncState; if (socketHandle.Connected) { socketHandle.EndConnect(ar);//结束挂起的异步连接请求 socketHandle.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null); } else { MessageBox.Show("连接失败"); } } /// <summary> /// 接收数据的回调 /// </summary> /// <param name="ar"></param> private void ReceiveCallBack(IAsyncResult ar) { int length=socket.EndReceive(ar);//结束挂起的异步读取 if (length > 0) { byte[] data = new byte[length]; Array.Copy(buffer, 0, data, 0, length); OnDataReceive(data); socket.BeginReceive(buffer, 0, buffer.Length,SocketFlags.None, new AsyncCallback(ReceiveCallBack), null); } else { } } /// <summary> /// 发送数据 /// </summary> /// <param name="data"></param> public void SendData(byte[] data) { if (socket != null) { socket.Send(data); } } /// <summary> /// 关闭 /// </summary> public void Close() { if (socket != null) { socket.Close(); } } }