日期:2014-05-18 浏览次数:21284 次
#region 成员变量 string _IP; int _port; byte[] recvBuf = null; string _receiveString; bool _hasReceive; UdpClient receiver = null; IPEndPoint senderEP = null; public string IP { get { return _IP; } set { _IP = value; } } public int port { get { return _port; } set { _port = value; } } public string receiveString { get { hasReceive = false; return _receiveString; } set { _receiveString = value; } } public bool hasReceive { get { return _hasReceive; } set { _hasReceive = value; } } #endregion 成员变量 #region 构造函数 public UdpReceiver(string IP,int port) { _IP = IP; _port = port; IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(_IP), _port); receiver = new UdpClient(localEP); _hasReceive = false; senderEP = new IPEndPoint(IPAddress.Any, 0); } #endregion 构造函数 #region 成员函数 public void receive() { recvBuf = receiver.Receive(ref senderEP); _receiveString = Encoding.Default.GetString(recvBuf); _hasReceive = true; } public void close() { if (receiver != null) receiver.Close(); } #endregion 成员函数
System.Timers.Timer resetReceiverTimer; double resetInterval = 60 * 1; UdpReceiver receiver = null; private void ResetReceiver(object sender, System.Timers.ElapsedEventArgs e) { receiveThread.Abort(); //接收线程,重新初始化套接字之前先关闭线程 receiver.close(); receiver = new UdpReceiver(IP, 42000); receiveThread = new Thread(new ThreadStart(loopReceive)); receiveThread.Start(); }