tcpclient连接超时怎么设置?

解决方案 »

  1.   

    TcpClient.SendTimeout 属性
    获取或设置 TcpClient 等待发送操作成功完成的时间量。
    属性值
    发送超时值(以毫秒为单位)。默认为 0。
    备注
    SendTimeout 属性确定 Send 方法在成功返回之前阻塞的时间量。这一时间的度量单位为毫秒。
    调用 Write 方法以后,基础 Socket 返回实际发送到主机的字节数。SendTimeout 属性确定 TcpClient 在接收返回的字节数之前所等待的时间量。如果超时在 Send 方法成功完成之前过期,TcpClient 将引发 SocketException。默认为无超时。
    示例
    [Visual Basic, C#] 以下示例设置和获取 SendTimeout 值。
    [Visual Basic] 
    ' Sets the send time out using the SendTimeout public property.
    tcpClient.SendTimeout = 5' Gets the send time out using the SendTimeout public property.
    If tcpClient.SendTimeout = 5 Then
       Console.WriteLine(("The send time out limit was successfully set " + tcpClient.SendTimeout.ToString()))
    End If
    [C#] 
    // sets the send time out using the SendTimeout public property.
    tcpClient.SendTimeout = 5;// gets the send time out using the SendTimeout public property.
    if (tcpClient.SendTimeout == 5)
         Console.WriteLine("The send time out limit was successfully set " +
         tcpClient.SendTimeout.ToString());
      

  2.   

    TcpClient.ReceiveTimeout 属性
    获取或设置在初始化一个读取操作以后 TcpClient 等待接收数据的时间量。
    属性值
    连接的超时值(以毫秒为单位)。默认值为 0。
    备注
    ReceiveTimeout 属性确定 Read 方法在能够接收数据之前保持阻塞状态的时间量。这一时间的度量单位为毫秒。如果超时在 Read 成功完成之前到期,TcpClient 将引发 SocketException。默认为无超时。
    示例
    [Visual Basic, C#] 以下示例设置和获取接收超时。
    [Visual Basic] 
    ' Sets the receive time out using the ReceiveTimeout public property.
    tcpClient.ReceiveTimeout = 5' Gets the receive time out using the ReceiveTimeout public property.
    If tcpClient.ReceiveTimeout = 5 Then
       Console.WriteLine(("The receive time out limit was successfully set " + tcpClient.ReceiveTimeout.ToString()))
    End If
    [C#] 
    // Sets the receive time out using the ReceiveTimeout public property.
    tcpClient.ReceiveTimeout = 5;// Gets the receive time out using the ReceiveTimeout public property.
    if (tcpClient.ReceiveTimeout == 5)
         Console.WriteLine("The receive time out limit was successfully set " +
         tcpClient.ReceiveTimeout.ToString());
      

  3.   

    谢谢 wangsaokui(无间道III(MSMVP)) 的回复
    忘了说了我是在 .NET Framework 精简版下用的,没有TcpClient.SendTimeout和TcpClient.ReceiveTimeout  属性
      

  4.   

    用异步线程A线程发送,B线程计时如果B线程时间到,A没有返回值,就中断A线程
      

  5.   

    在异步调用的时候
    socketEvent.WaitOne(5000,true);
    超过等待时间就退出重新执行