M
Markus Freitag
Guest
Hello,
I found this code in the forum.
My goal is to send a request, to wait for the answer. I have 5 attempts to receive the response. How do I add the timeout correctly to read?
Please see // ######## Timeout Is not waiting ####
stream.Read(bytes, 0, bytes.Length); // ### Wait
Regards Markus
static void Main(string[] args)
{
int TimeoutReadMessage = 10;
string ip = "IP address";
int port = Port;
TcpClient client = new TcpClient();
client.Connect(ip, port);
NetworkStream stream = client.GetStream();
stream.ReadTimeout = TimeoutReadMessage * 1000; //Set a 10 millisecond timeout for reading.
stream.WriteTimeout = TimeoutReadMessage * 1000;
string message = "welcome beijing";
byte[] myWriteBuffer;
if (stream.CanWrite)
{
myWriteBuffer = Encoding.ASCII.GetBytes(message + "\n");
stream.Write(myWriteBuffer, 0, myWriteBuffer.Length);
stream.Flush();
}
byte[] bytes = new byte[1024];
stream.Read(bytes, 0, bytes.Length); // ######## Timeout Is not waiting ####
string result = "";
foreach (var item in bytes)
{
char cha = Convert.ToChar(item);
result += cha.ToString();
}
Console.ReadKey();
}
Continue reading...
I found this code in the forum.
My goal is to send a request, to wait for the answer. I have 5 attempts to receive the response. How do I add the timeout correctly to read?
Please see // ######## Timeout Is not waiting ####
stream.Read(bytes, 0, bytes.Length); // ### Wait
Regards Markus
static void Main(string[] args)
{
int TimeoutReadMessage = 10;
string ip = "IP address";
int port = Port;
TcpClient client = new TcpClient();
client.Connect(ip, port);
NetworkStream stream = client.GetStream();
stream.ReadTimeout = TimeoutReadMessage * 1000; //Set a 10 millisecond timeout for reading.
stream.WriteTimeout = TimeoutReadMessage * 1000;
string message = "welcome beijing";
byte[] myWriteBuffer;
if (stream.CanWrite)
{
myWriteBuffer = Encoding.ASCII.GetBytes(message + "\n");
stream.Write(myWriteBuffer, 0, myWriteBuffer.Length);
stream.Flush();
}
byte[] bytes = new byte[1024];
stream.Read(bytes, 0, bytes.Length); // ######## Timeout Is not waiting ####
string result = "";
foreach (var item in bytes)
{
char cha = Convert.ToChar(item);
result += cha.ToString();
}
Console.ReadKey();
}
Continue reading...