Пример #1
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="bytes">数据字节</param>
 private void Send(byte[] bytes)
 {
     try
     {
         _socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, asyncResult =>
         {
             try
             {
                 int length = _socket.EndSend(asyncResult);
                 HandleSendMsg?.BeginInvoke(bytes, this, _server, null, null);
             }
             catch (Exception ex)
             {
                 HandleException?.BeginInvoke(ex, null, null);
             }
         }, null);
     }
     catch (Exception ex)
     {
         HandleException?.BeginInvoke(ex, null, null);
     }
 }
Пример #2
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="bytes">数据</param>
        /// <param name="iPEndPoint">目标地址</param>
        public void Send(byte[] bytes, IPEndPoint iPEndPoint)
        {
            try
            {
                _udpClient.BeginSend(bytes, bytes.Length, iPEndPoint, asyncCallback =>
                {
                    try
                    {
                        int length = _udpClient.EndSend(asyncCallback);

                        HandleSendMsg?.BeginInvoke(this, iPEndPoint, bytes, null, null);
                    }
                    catch (Exception ex)
                    {
                        HandleException?.BeginInvoke(ex, null, null);
                    }
                }, null);
            }
            catch (Exception ex)
            {
                HandleException?.BeginInvoke(ex, null, null);
            }
        }