Exemplo n.º 1
0
 /// <summary>
 /// 数据接收回调方法
 /// </summary>
 /// <param name="ar">回调参数</param>
 private void OnReceive(IAsyncResult ar)
 {
     try
     {
         int real_recv = _socket.EndReceiveFrom(ar, ref remoteEndPoint);
         if (real_recv >= 1)
         {
             if (UDPMessageReceived != null)
             {
                 UDPMessageReceivedEventArgs args = new UDPMessageReceivedEventArgs();
                 args.Msg        = (Msg)_buffer4recv[0];
                 args.RemoteIP   = (remoteEndPoint as IPEndPoint).Address.ToString();
                 args.RemotePort = (remoteEndPoint as IPEndPoint).Port;
                 args.Time       = DateTime.Now;
                 args.Data       = new byte[real_recv - 1];
                 Buffer.BlockCopy(_buffer4recv, 1, args.Data, 0, real_recv - 1);
                 UDPMessageReceived(_client_id, args);
             }
         }
         _socket.BeginReceiveFrom(_buffer4recv, 0, 1024 * 64, SocketFlags.None, ref remoteEndPoint, new AsyncCallback(OnReceive), remoteEndPoint);
     }
     catch
     {
     }
 }
 /// <summary>
 /// 接受消息
 /// </summary>
 /// <param name="csID"></param>
 /// <param name="args"></param>
 void manager_UDPMessageReceived(string csID, UDPMessageReceivedEventArgs args)
 {
     this.Invoke((Action)delegate()
     {
         if (args.Msg == Msg.Zmsg1)
         {
             textBox1.AppendText(args.Time.ToLongTimeString() + " " + args.RemoteIP + ":" + args.RemotePort + " 发送文本:\n"
                 + Encoding.Unicode.GetString(args.Data) + "\n");
         }
     });
 }
Exemplo n.º 3
0
        /// <summary>
        /// 数据接收回调方法
        /// </summary>
        /// <param name="ar">回调参数</param>
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                int real_recv = _socket.EndReceiveFrom(ar, ref remoteEndPoint);
                if (real_recv >= 1)
                {
                    if (UDPMessageReceived != null)
                    {
                        UDPMessageReceivedEventArgs args = new UDPMessageReceivedEventArgs();
                        args.Msg = (Msg)_buffer4recv[0];
                        args.RemoteIP = (remoteEndPoint as IPEndPoint).Address.ToString();
                        args.RemotePort = (remoteEndPoint as IPEndPoint).Port;
                        args.Time = DateTime.Now;
                        args.Data = new byte[real_recv - 1];
                        Buffer.BlockCopy(_buffer4recv, 1, args.Data, 0, real_recv - 1);
                        UDPMessageReceived(_client_id, args);
                    }
                }
                _socket.BeginReceiveFrom(_buffer4recv, 0, 1024 * 64, SocketFlags.None, ref remoteEndPoint, new AsyncCallback(OnReceive), remoteEndPoint);
            }
            catch
            {

            }
        }