Receivetimeout of tcpclient

Source: Internet
Author: User

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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.