Discuss the problem of using networkstream to collect incomplete data, list the current methods, and find the best solution

Source: Internet
Author: User
Tcpclient =NewTcpclient ();

//Get stream for read/write
//Write preset Information
Networkstream streamserver = tcpclient. getstream ();
Streamserver. Write (packagecontent,0, Packagecontent. Length );

//Read returned data
......

 

The following two methods are used to read the returned data: incomplete collection
Method 1:

While(I = streamserver. Read (bytes,0, Bytes. Length ))! =0)
{
For(IntJ =0; J <I; j ++)
{
List. Add (Bytes [J]);
}
}

 

Method 2:

 
Do
{
I = streamserver. Read (bytes,0, Bytes. Length );
For(IntJ =0; J <I; j ++)
{
List. Add (Bytes [J]);
}

}
While(Streamserver. dataavailable );

 

The above two methods are ideal for normal use. The problem of deploying a single machine or LAN on the client and server is not obvious. Generally, they can handle all the problems, but in the Internet environment, for example, jquery. JS is not fully collected (its own files are also relatively large). The principle is theoretically networkstream. when the actual MPs queue is empty (network delay occurs because the MPs queue is empty, and data is not returned for some time on the server ), if the read Data byte length is 0 or the dataavailable attribute is false, the loop is exited if the data is not fully read.

The following methods can reduce this impact to a certain extent:

 
Streamserver. readtimeout =500;
Do
{
Try
{
I = streamserver. Read (bytes,0, Bytes. Length );
For(IntJ =0; J <I; j ++)
{
List. Add (Bytes [J]);
}
}
Catch
{

I =0;
}

}
While(I>0);

 

That is, set a read timeout time for networksteam. If the read timeout time is reached, the server is deemed to have finished writing and no data is returned. Set this value to 100 or the csdn logon page is incomplete, set it to 500.

This solution is not good, it will lead to longer service response time and a better solution. This is a common problem. You are welcome to discuss it.

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.