C#UDP通讯UdpClient

  • Post author:
  • Post category:其他



远程主机强迫关闭了一个现有的连接 红色部分很重要

class UdpClientClass
    {
        /// <summary>
        /// 构建客户端
        /// </summary>
        /// <param name="servierIpAddress">服务器iP地址或者域名</param>
        /// <param name="sevierPort">服务器监听端口</param>
        /// <param name="locadPort">本地监听端口</param>
        /// <param name="timeOut">超时等待时间</param>
        public UdpClientClass(string servierIpAddress, int sevierPort, int locadPort, int timeOut)
        {
            if (FpHelper.CheckIpAddress(ref servierIpAddress) == true)
            {
                try
                {
                    ServerIPE = new IPEndPoint(IPAddress.Parse(servierIpAddress), sevierPort);
                    UdpListenClient = new UdpClient(locadPort);//固定通信端口
                    UdpListenClient.Client.ReceiveTimeout = 3000;
                    const long IOC_IN = 0x80000000;
                    const long IOC_VENDOR = 0x18000000;
                    const long SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;

                    byte[] optionInValue = { Convert.ToByte(false) };
                    byte[] optionOutValue = new byte[4];

                    UdpListenClient.Client.IOControl((IOControlCode)SIO_UDP_CONNRESET, optionInValue, optionOutValue);

                   
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("绑定端口失败" + ex.Message.ToString());
                }

            }
        }

        /// <summary>
        /// UDP发送类,绑定了一个固定的端口
        /// </summary>
        private static UdpClient UdpListenClient;

        public event EventHandler MessageChanged;

        /// <summary>
        /// 接收到服务器消息改变后触发的事件
        /// </summary>
        /// <param name="user"></param>
        public void OnMessageChanged(UdpClientClass fp)
        {
            if (MessageChanged != null)
            {
                MessageChanged(fp, null);
            }
        }

        /// <summary>
        /// 服务器端的IP与端口
        /// </summary>
        private IPEndPoint ServerIPE = null;

        bool IsReceiving = false;

        public void Send(byte[] data, int len)
        {
          int len1 =  UdpListenClient.Send(data, len, ServerIPE);
            if (!IsReceiving)
                StartAndLsn();
        }

        private Thread ClientRecThread;

        private void StartAndLsn()
        {
            IsReceiving = true;
            ClientRecThread = new Thread(new ThreadStart(RecF));//启动新线程做接收
            ClientRecThread.IsBackground = true;
            ClientRecThread.Start();

        }//启动并且 监听 服务器发来的数据


        private void RecF()//接收数据做服务
        {
            byte[] btRec = null;
            while (IsReceiving)
            {
                IPEndPoint remoteIPE = new IPEndPoint(IPAddress.Any, 0);
                btRec = UdpListenClient.Receive(ref remoteIPE);//UDP接收数据
                if (btRec.Length > 0 && remoteIPE.Address == ServerIPE.Address)//只处理特定的服务端的数据
                {
                    System.Windows.Forms.MessageBox.Show("res");
                }
                else
                {
                }

            }
        }//循环接收数据
    }