我正在使用TcpClient连接到套接字,下面是示例代码,我连接到套接字一次以保留conenction会话ID,然后我继续从套接字发送和接收消息,直到我找到一个有效的消息,然后我停下来:
// client is a global object to retain connection session id
TcpClient client = new TcpClient("127.0.0.1", 1800);
public void Connect()
{
message = "First message"; // first message
do
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
data = new Byte[256];
String responseData = String.Empty;
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
} while (message = "valid") // loop untill message is valid
stream.Close();
client.Close();
}
我的问题是如何在我的代码中有效地使用TcpClient.ReceiveTimeout和TcpClient.SentTimeout 20秒?
我是否使用TcpClient连接做正确的事情以保留连接会话ID?
谢谢。