/// <summary> /// <param name="remoteIpAddress">远程服务器地址</param> /// <param name="remotePort">远程端口</param> /// <param name="localPort">本地端口</param> /// </summary> public AsyncUdpClient(IPAddress remoteIpAddress, int remotePort, int localPort) { receiveUdpState = new UdpState(new UdpClient(), new IPEndPoint(remoteIpAddress, remotePort)); sendUdpState = new UdpState(new UdpClient(new IPEndPoint(IPAddress.Any, localPort)), new IPEndPoint(IPAddress.Any, localPort)); sendUdpState.Client.Connect(sendUdpState.EndPoint); BeginReceiveData(); }
public void DefaultSendCallback(IAsyncResult asyncResult) { UdpState state = asyncResult.AsyncState as UdpState; if (asyncResult.IsCompleted) { int sendSize = state.Client.EndSend(asyncResult); SendCallBack?.Invoke(sendSize); } }
public void DefaultReceiveCallback(IAsyncResult asyncResult) { UdpState state = asyncResult.AsyncState as UdpState; if (asyncResult.IsCompleted) { Byte[] receiveBytes = state.Client.EndReceive(asyncResult, ref state.EndPoint); if (ReceiveCallBack == null || !ReceiveCallBack(receiveBytes)) { BeginReceiveData(); } } }