//发送部分
 public void Send(String sMsg)
        {
            this.receive_ip();            
            System.Net.IPEndPoint iep = new System.Net.IPEndPoint(IPAddress.Parse(s_address), i_port);
            th = new Thread(new ThreadStart(receive_ip));
            th.IsBackground = true;
            th.Start();
            th.join(2500);
            byte[] a = Encoding.UTF8.GetBytes(sMsg);
            uc.Send(a, a.Length, iep);                    
        }
//接收部分
 public string Receive()
        {
            while (true)
            {               
                Data = null;                                
                if (th.IsAlive)
                {
                    uc.Close();
                    uc = new System.Net.Sockets.UdpClient();
                    Data = "timeout";
                }
                else
                {
                    Data = Encoding.UTF8.GetString(uc.Receive(ref iep));
                }                
                return Data;
            }
        } 
我这个方法检测不到TIMEOUT,各位还有什么其他方法吗?       

解决方案 »

  1.   

    异步的还需要检测超时?下面的代码是同步的,摘自#SNMP/// <summary>
            /// Sends this <see cref="GetRequestMessage"/> and handles the response from agent.
            /// </summary>
            /// <param name="timeout">Timeout.</param>
            /// <param name="port">Port number.</param>
            /// <returns></returns>
            public IList<Variable> Send(int timeout, int port)
            {
                byte[] bytes = _bytes;
                IPEndPoint agent = new IPEndPoint(_agent, port);
                using (UdpClient udp = new UdpClient())
                {
                    udp.Send(bytes, bytes.Length, agent);
                    IPEndPoint from = new IPEndPoint(IPAddress.Any, 0);
                    IAsyncResult result = udp.BeginReceive(null, this);
                    result.AsyncWaitHandle.WaitOne(timeout, false);
                    if (!result.IsCompleted)
                    {
                        throw SharpTimeoutException.Create(_agent, timeout);
                    }
                    
                    bytes = udp.EndReceive(result, ref from);
                    udp.Close();
                }
                
                MemoryStream m = new MemoryStream(bytes, false);
                ISnmpMessage message = MessageFactory.ParseMessages(m)[0];
                if (message.TypeCode != SnmpType.GetResponsePdu)
                {
                    throw SharpOperationException.Create("wrong response type", _agent);
                }
                
                GetResponseMessage response = (GetResponseMessage)message;
                if (response.SequenceNumber != SequenceNumber)
                {
                    throw SharpOperationException.Create("wrong response sequence", _agent);
                }
                
                if (response.ErrorStatus != ErrorCode.NoError)
                {
                    throw SharpErrorException.Create(
                        "error in response",
                        _agent,
                        response.ErrorStatus,
                        response.ErrorIndex,
                        response.Variables[response.ErrorIndex - 1].Id);
                }
                
                return response.Variables;
            }仅作参考。
      

  2.   

    我的代码是发到一个无线设备上,然后这个设备就返回一个代码,假如我SEND到设备上,但超时也拿不到回应怎么办?
      

  3.   

                    if (!result.IsCompleted)
                    {
                        throw SharpTimeoutException.Create(_agent, timeout);
                    }发送的函数会抛出超时异常。上面的代码只要正确处理这个异常就好了。收到回复,一切正常,没收到的话,在catch里面处理异常好了。
      

  4.   

    Lz说的超时是Server的包超时clientm没有收到还是 Client的回复包超时Server没有收到