Beispiel #1
0
        /// <summary>
        ///     接收到信息的回调函数
        /// </summary>
        /// <param name="iar"></param>
        private void EndReceiveFromCallback(IAsyncResult iar)
        {
            int byteRead = 0;

            try
            {
                //完成接收
                byteRead = state.WorkSocket.EndReceiveFrom(iar, ref state.RemoteEP);
            }
            catch
            {
            }
            if (byteRead > 0)
            {
                state.IpEndPoint = (IPEndPoint)state.RemoteEP;
                byte[] haveDate = ReceiveDateOne.DateOneManage(state, byteRead); //接收完成之后对数组进行重置
                BeginReceiveFrom();
                int havePort = UdpPortSetGet.GetPort(ref haveDate);
                if (havePort != 0)
                {
                    state.IpEndPoint.Port = havePort;
                }
                StateCode statecode = ReceiveDateDistribution.Distribution(haveDate);
                codeManage(state, statecode);
            }
            else
            {
                BeginReceiveFrom();
            }
        }
Beispiel #2
0
        /// <summary>
        /// 最基础的Udp异步发送
        /// </summary>
        /// <param name="stateBase">StateBase</param>
        /// <param name="bytes">数据</param>
        override internal void Send(TransmitData stateBase, byte[] bytes)
        {
            if (stateBase == null)
            {
                return;
            }
            EndPoint remoteEndPoint = (EndPoint)stateBase.IpEndPoint;

            byte[] sendDate = UdpPortSetGet.SetPort(Port, bytes);
            try
            {
                state.WorkSocket.BeginSendTo(sendDate, 0, sendDate.Length, 0, remoteEndPoint, new AsyncCallback(SendCallback), state.WorkSocket);
            }
            catch { }
            #region 异步发送的另一种方法
            //try
            //{
            //    EndPoint remoteEndPoint = (EndPoint)stateBase.IpEndPoint;
            //    state.SendSocketArgs.SetBuffer(bytes, 0, bytes.Length);
            //    state.SendSocketArgs.RemoteEndPoint = remoteEndPoint;
            //   bool bb= state.WorkSocket.SendToAsync(state.SendSocketArgs);
            //   if (bb == false)
            //   {
            //       state.SendSocketArgs = new SocketAsyncEventArgs();
            //       Send(stateBase, bytes);
            //   }
            //}
            //catch {}
            #endregion
        }