Problems encountered by Java using the socket transfer file (GO)

Source: Internet
Author: User

1. Wrote a socket transfer file program, found that the transmission of past files have a problem. Looking for an afternoon finally seemed to find the reason, record down warning:

Accept one end of the file and use Thread.Sleep (time) to take a break before writing the file to the local to resolve the problem.

Personally, it may be in the transmission process, the receiving end to disk write speed is a bit slow, is caused by the subsequent overwrite error.

//------------------------------------------------------------------------------------------------------------- -------

12-29: Recently read the book <<java TCP/IP Socket programming >> It seems to understand the cause of the title problem:

It is not possible to assume any consistency between writing data to the output stream at one end of the connection and data being read from the input stream at the other end. Although the sender is sent at 1024bytes for the unit, but because the network speed problem receiving end is not necessarily in accordance with the 1024bytes unit accepted, may be less than 1024. So buf[0]=5 this method is not feasible, the receiver receives the buff's first character may not be 5.

Note: If the buffer is sent and received in 1024bytes.

//------------------------------------------------------------------------------------------------------------- --------

12-29 Nights: I know the reason thoroughly. is the network transmission problem, the sending side is sent in 1024byte units, the receiving end is accepted in 1024byte. However, due to the network transmission speed limit, the sender sends out the 1024byte can not reach the receiving end at the same time, and the receiving side uses read original may be less than 1024byte. The next time you continue reading from the next position you read, it is wrong to set the beginning of the 1024byte array to the identity bit. The solution is to Thread.Sleep (mileseconds) before each read on the receiving end; This gives you enough time to wait for the data sent by the sender to fully reach the receiving end and then accept it, so that the value of buff[0] can be the most marked bit.

However, this setting buff[0] is not advocated as a way to represent bits. The good practice is that the receiving side receives the request to send the file, accepts the file, does not consider the identity, there is no identity bit, the sending end sends the file to call Close (), to send the end identity, the receiving end will receive through read ()-1. The receive side closes the accept stream. , the file transfer process is complete.

Http://www.cnblogs.com/wangjiyuan/p/3493186.html

Problems encountered by Java using the socket transfer file (GO)

Related Article

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.