Q:
I implement the READ function to read data three times in a loop if the receivetimeout does not get the data within the specified time. It is expected that each READ function will wait for the receivetimeout time.
However, the actual situation is that only after the first wait for such a long time, the read will be returned immediately and no data is read. Please give answers. Urgent !! How does the receivetimeout attribute expire after the second time?
MyCodeProbably:
Tcpclient _ tcpsocket = NULL;
Networkstream _ stream2server;
_ Tcpsocket = new tcpclient (bhsservername, bhsserverport );
_ Tcpsocket. receivetimeout = bhswaitprtimeout;
_ Stream2server = _ tcpsocket. getstream ();
Int I = 0;
Byte [] _ buffer = new byte [200];
While (I <3)
{
Int bytesread = _ stream2server. Read (_ buffer, 0, _ buffer. Length );
I ++;
}
A.
networkstream has a dataavailable read-only attribute
when the first read fails, this attribute is automatically changed to false
so the loop body should be changed to
while (I <3)
{< br> If (dataavailable)
{< br> int bytesread = _ stream2server. read (_ buffer, 0, _ buffer. length);
I ++;
}< br> else
sleep (3000);
}