Accustomed to TCP programming, it is wrong to think that UDP can handle these problems. A udp application must take full responsibility for reliability, including packet loss, duplication, latency, out-of-order, and connection failure.
Generally, we develop and test on a LAN with good reliability and low transmission latency. Some problems are not easy to expose, but errors may occur on the large internet.
The UDP protocol pushes the responsibility for delivery reliability to the upper layer, that is, the application layer. Below, several classes are compiled to specifically deal with two problems: out-of-order and packet loss.
Four classes: DataPacket class, PacketHeader class, PacketBody class, and DataEntry class, which are located in the same file DataPacket. java.
The DataPacket class is equivalent to a facade mode and is provided for external use. Communication data is also processed in this class.
Package com. skysoft. pcks;
Import java. io .*;
Import java.net .*;
Import java. util .*;
Public class DataPacket {
InputStream is;
OutputStream OS;
PacketHeader;
PacketBody body;
ArrayList al;
Public static final int DataSwapSize = 64532;
/**
* Used to receive a datagram
*/
Public DataPacket (){
Header = new PacketHeader ();
Body = new PacketBody ();
Al = new ArrayList ();
}
/**
* Used to send data reports. It calls the message segmentation operation.
* @ Param file String Hard Drive file
*/
Public DataPacket (String file ){
This ();
Try {
Is = new FileInputStream (file );
Header. CalcHeaderInfo (is. available ());
This. madeBody ();
Is. close ();
// This. Gereratedata ();
}
Catch (FileNotFoundException ex ){
Ex. printStackTrace ();
}
Catch (IOException ex1 ){
Ex1.printStackTrace ();
}
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.