Exemple #1
0
        /// <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();
        }
Exemple #2
0
        public void DefaultSendCallback(IAsyncResult asyncResult)
        {
            UdpState state = asyncResult.AsyncState as UdpState;

            if (asyncResult.IsCompleted)
            {
                int sendSize = state.Client.EndSend(asyncResult);
                SendCallBack?.Invoke(sendSize);
            }
        }
Exemple #3
0
        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();
                }
            }
        }