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.