如何设置TcpClient的超时时间?
我有一个TcpClient,用于将数据发送到远程计算机上的侦听器。远程计算机有时会打开,有时会关闭。因此,TcpClient将经常无法连接。我希望TcpClient一秒钟后超时,因此当它无法连接到远程计算机时不需要花费很多时间。当前,我将以下代码用于TcpClient: try { TcpClient client = new TcpClient("remotehost", this.Port); client.SendTimeout = 1000; Byte[] data = System.Text.Encoding.Unicode.GetBytes(this.Message); NetworkStream stream = client.GetStream(); stream.Write(data, 0, data.Length); data = new Byte[512]; Int32 bytes = stream.Read(data, 0, data.Length); this.Response = System.Text.Encoding.Unicode.GetString(data, 0, bytes); stream.Close(); client.Close(); FireSentEvent(); //Notifies of success } catch (Exception ex) { FireFailedEvent(ex); …