Java uses TCP and UDP to transmit files

Source: Internet
Author: User

Introduction
The purpose of this project is to implement the function of transferring a file between different hosts through network connections between two applications. The two applications should use UDP and TCP Protocols respectively to transmit at least 1 MB of files.
Implementation and description
Source code

Both applications are implemented by a single program with source code.

Description

The program uses the following command line for compilation:

javac *.java

Run the following two command lines:

Aggreger:

# java FileReceiver [protocol] [port]

Sender:

# java FileSender [protocol] [host] [port] [filename]

The [Protocol] parameter can be "UDP" or "TCP", but the sender and receiver must be consistent.

The file will be generated in the directory started by the attacker. The default name is "stored ed-[filename]".
TCP implementation
Implementation Overview
In TCP implementation, the consumer er opens a serversocket and listens to the defined port. After the sender is started, a new socket is opened for the inputstream and outputstream objects at both ends of the socket.
An initial message containing the file name and size will be sent to the receiver by the sender. In this way, the receiver can use a meaningful name to store the received file and determine when the file is completely transferred. This information is not required. When the receiver cannot receive files, the sender will stop consuming unnecessary bandwidth.
The file transmits the read data through a fileinputstream object, and then writes the data to an outputstream object returned by the socket. To improve application efficiency, each data read and relay is 8 KB, and a byte array is used as the cache.
TCP experience
Practice has proved that TCP file transmission is simple and reliable. The program's efficiency depends on the cache size used, but the transferred files are accurately received and saved in all executed tests.
UDP implementation
Implementation Overview
The implementation of UDP file transmission uses the standard Java datasync class: datagrampacket and datagramsocket.
When the receiver is executed, it opens a socket with the specified port number and waits to listen for incoming packets. After the sender is started, it opens a socket connected to the specified host and port, and transmits a single packet containing the file name and the file size to be transmitted. After the package is sent, the socket will wait and listen to the package.
Based on the received initial package, the receiver creates an outputstream object for the file and sends a package containing the "OK" Word to the monitored sender. After receiving the "OK" packet, the sender starts to read the file content and sends it through the UDP packet, each time containing 512 bytes. The receiver writes these blocks to the file in the order they are received and receives them again until the received bytes reach the expected number. Then the program is terminated.
UDP experience
UDP is an unreliable protocol for transmitting continuous data. This means packet loss occurs during transmission, and the packet receiving order is random. The above example does not solve these problems in file transmission. This means that the above applications are not correct and complete at each running time (the obtained files. The following describes the causes of two frequent problems and the feasible solutions.
If the receiving order of the two packages is incorrect during file transmission, the order of file writing is in the order of receipt. This will cause damage to the received file. The solution to this problem is to define a serial number for each transmission. This allows the receiver to store these packages in the correct order, regardless of the order in which they arrive.
If packet loss occurs during file transfer, the volume er cannot receive the expected amount of data. In the above example, this will cause the worker to continue running and wait for the remaining data. A feasible solution to this problem is that the caller performs each transmission after a given time span, and the call times out. But in order to make this request purposeful, we need to extend the serial number for the package as mentioned above. Otherwise, we will not be able to receive a given amount of data and will be limited to the full transfer of the request file.
Another solution to these two problems is to send a receiving request to the sender after receiving the packets correctly each time. This method eliminates the possibility of packet loss, but slows down transmission.
Conclusion

The above implementation makes it feasible to transfer files between hosts. However, if the UDP protocol is used, we cannot guarantee the integrity of the file and the correctness of the receipt (sequence. We provide a general description of solving these problems, but in actual file transmission, the simplest solution to these problems is to replace UDP with TCP.

Http://www.daimi.au.dk /~ Alex/ddist/project1.php.

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.