Self-addressing sockets (Datagram Sockets)
Because each connection that uses a stream socket takes a certain amount of time, to reduce this overhead, the network API provides a second socket: A datagram socket, which sends addressing information using UDP (from a client program to a service program or from a service program to a client program), The difference is that you can send multiple IP packets through a self addressed socket, the self addressing information is included in the self addressing package, and the self addressed package is included in the IP packet, which limits the length of the addressing to 60000 bytes. Figure 2 shows the self addressing information for the self addressed package located within the IP packet.
Unlike TCP, which guarantees that information arrives at an information destination, UDP provides another method, and if the self addressing packet does not reach its destination, then UDP does not request the sender to resend the self addressed packet because UDP contains error detection information in each of the self-addressing packages. UDP only makes simple error checking after each addressable packet arrives at the destination. If the detection fails, UDP discards the self addressing package and does not request replacements from the sender, which is similar to sending a letter through the post Office, and the sender does not need to establish a connection with the recipient until the letter is sent. There is no guarantee that the letter will reach the addressee.
The self-addressing socket work consists of the following three classes: Datagrampacket, Datagramsocket, and MulticastSocket. The Datagrampacket object depicts the address information of the self addressed package, Datagramsocket represents the client program and the service program self addressing socket, and multicastsocket depicts a addressable socket capable of multicast. These three classes are located in the java.net package.
Datagrampacket class
Before using a self addressing package, you need to familiarize yourself with the Datagrampacket class, address information, and the self-addressing package in a byte array and simultaneously compress into objects created by this class
Datagrampacket has several constructors, and even though they form different forms, they usually have two common parameters: the byte [] buffer and the int length,buffer parameters contain a reference to a byte array that holds the message of the addressable packet. Length represents the size of the byte array.
The simplest constructor is Datagrampacket (byte [] buffer, int length), which determines the length of the array of self addressable packets and arrays, but does not have address and port information for any of the addressable packets. This information can be added later by calling method Setaddress (inetaddress addr) and setport (int port), and the following code demonstrates these functions and methods.
byte [] buffer = new byte [100];
DatagramPacket dgp = new DatagramPacket (buffer, buffer.length);
InetAddress ia = InetAddress.getByName ("www.disney.com");
dgp.setAddress (ia);
dgp.setPort (6000); // Send datagram packet to port 6000.
If you prefer to include both the address and the port number when calling the constructor, you can use the Datagrampacket (byte [] buffer, int length, inetaddress addr, int port) function, and the following code demonstrates an alternative.
byte [] buffer = new byte [100];
InetAddress ia = InetAddress.getByName ("www.disney.com");
DatagramPacket dgp = new DatagramPacket (buffer, buffer.length, ia,
6000);
Sometimes you want to change the byte array and its length after creating the Datagrampacket object, which can be done by calling SetData (byte [] buffer) and setlength (int length). You can get a reference to a byte array at any time by calling GetData () to get the length of the byte array by calling GetLength (). The following code demonstrates these methods:
byte [] buffer2 = new byte [256];
dgp.setData (buffer2);
dgp.setLength (buffer2.length);
For more information on Datagrampacket, please refer to the SDK documentation.