示例#1
0
        // 发送回调函数
        private void SendCallback(IAsyncResult iar)
        {
            UdpState udpState = iar.AsyncState as UdpState;

            //Console.WriteLine("第{0}个请求处理完毕!", udpState.counter);
            //Console.WriteLine("number of bytes sent: {0}", udpState.udpClient.EndSend(iar));
            udpState.udpClient.EndSend(iar);
            sendDone.Set();
        }
示例#2
0
        // 接收回调函数
        private void ReceiveCallback(IAsyncResult iar)
        {
            UdpState udpReceiveState = iar.AsyncState as UdpState;

            if (iar.IsCompleted)
            {
                Byte[] receiveBytes  = udpReceiveState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint);
                string receiveString = Encoding.ASCII.GetString(receiveBytes);
                Console.WriteLine("Received: {0}", receiveString);
                //Thread.Sleep(100);
                receiveDone.Set();
                //SendMsg();
            }
        }
示例#3
0
        // 定义套接字
        //private Socket receiveSocket;
        //private Socket sendSocket;

        public AsyncUdpClient()
        {
            // 本机节点
            ipEndPoint = new IPEndPoint(IPAddress.Any, listenPort);
            // 远程节点
            remoteEP = new IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0], remotePort);
            // 实例化
            udpReceive = new UdpClient(ipEndPoint);
            udpSend    = new UdpClient();

            // 分别实例化udpSendState、udpReceiveState
            udpReceiveState            = new UdpState();
            udpReceiveState.udpClient  = udpReceive;
            udpReceiveState.ipEndPoint = ipEndPoint;

            udpSendState            = new UdpState();
            udpSendState.udpClient  = udpSend;
            udpSendState.ipEndPoint = remoteEP;
        }