Is this bad programming ?
DateTime dtExpire = DateTime.Now.AddSeconds(90);
while (client.Connected && DateTime.Now < dtExpire)
{
if (client.Available == 0) continue;
//or can also use: if (!networkStream.DataAvailable) continue;
dtExpire = DateTime.Now.AddSeconds(30);
//now do stuff with client via stream
}
The goal being to insure that the client does not take too mor time than the server is willing to wait to pocess incoming messages. Of course, this code is inside of a Try/Catch block, as well as a Using Stream block, so the server would gracefully handle dropped connections or any other socket exceptions.
Basically, I just want to know if there’s a better way to handle this. Thanks.
Use the
ReceiveTimeoutproperty to specify how long to wait for an incoming message. When you use theReceivemethod (or its family of methods) and a timeout occurs, aSocketExceptionwill be thrown.Your code will be more complex if you have to accomplish this asynchronously, but it doesn’t look like you are.
Receiveby itself should do the job as it will block on the current thread.