There are times that the recByte will fail to receive response even though the server
did reply because the server wasn’t fast enough. Which if happened I use
catch (System.IO.IOException ioe)
to prevent the error, but are there possible alternatives that allow the client to wait a bit longer?
I did try
clientStream.ReadTimeout = 20000;
but it didn’t work
TcpClient Client = new TcpClient();
Client.Connect(ipaddress, portnum);
NetworkStream clientStream = Client.GetStream();
byte[] buffer = themessagetosent;
clientStream.Write(buffer, 0, buffer.Length);
byte[] recByte = theresponse;
clientStream.Read(recByte, 0, recByte.Length);
You need to increase timeout for the tcpClient, not for the stream. Also, probably want to increase timeout for both
sendandreceive, and probably to more than just 2 sec.I believe if you look at inner exception in IOException, you will see SocketException inside. Look at error code to find out what exactly is happening.