Transferred from: http://book.51cto.com/art/201203/322540.htm
Java uses datagramsocket on behalf of the UDP protocol Socket,datagramsocket itself is just a pier, not maintaining state, can not generate IO stream, its only role is to receive and send datagrams, Java uses Datagrampacket to represent datagrams, and the data that Datagramsocket receives and sends is done through Datagrampacket objects.
First look at the Datagramsocket constructor.
Datagramsocket (): Creates an Datagramsocket instance and binds the object to a native default IP address, a randomly selected port in all available ports in this computer.
Datagramsocket (int prot): Creates an Datagramsocket instance and binds the object to the native default IP address, specifying the port.
Datagramsocket (int port, inetaddress laddr): Creates a Datagramsocket instance and binds the object to the specified IP address, specifying the port.
A Datagramsocket instance can be created from any of the three constructors above, typically creating a Datagramsocket instance of the specified port when the server is created-so that other clients can send data to that server. Once the Datagramsocket instance has been obtained, it is possible to receive and send the data in the following two ways.
Receive (Datagrampacket P): Receives datagrams from this datagramsocket.
Send (Datagrampacket p): Sends the datagram outward with the Datagramsocket object.
As can be seen from the above two methods, using Datagramsocket to send the datagram, Datagramsocket does not know where to send the datagram, but is determined by the datagrampacket itself to determine the destination of the datagram. Just as the pier does not know the destination of each container, the terminal simply sends the containers out, and the container itself contains the container's destination.
Here's a look at the Datagrampacket constructor.
Datagrampacket (byte[] buf,int length): Creates a Datagrampacket object with an empty array, which is the object of receiving data from Datagramsocket.
Datagrampacket (byte[] buf, int length, inetaddress addr, int port): Creates a Datagrampacket object in an array containing data. The IP address and port are also specified when the Datagrampacket object is created-this determines the destination of the datagram.
Datagrampacket (byte[] buf, int offset, int length): Creates a Datagrampacket object with an empty array and specifies that the received data is placed in the BUF array, starting with offset, Put up to a maximum of length bytes.
Datagrampacket (byte[] buf, int offset, int length, inetaddress address, int port): Creates a Datagrampacket object for sending, Specifies that the send BUF array begins with offset, with a total length of bytes.
When the Client/server program uses the UDP protocol, there is actually no obvious server side and client, because both parties need to first establish a Datagramsocket object to receive or send datagrams, The Datagrampacket object is then used as a carrier for transmitting data. A program that usually has a fixed IP address and a fixed port Datagramsocket object is called a server because the Datagramsocket can proactively receive client data.
Before receiving data, the first or third constructor above should be used to generate a Datagrampacket object, giving the byte array to receive the data and its length. Then call Datagramsocket's receive () method to wait for the datagram to arrive, and receive () waits until the thread that called the method is blocked until a datagram is received. As shown in the following code:
// Create a Datagrampacket object that receives data Datagrampacket packet=New datagrampacket (buf,N.) ; // receiving datagrams Socket.receive (packet);
Before sending the data, call the second or fourth constructor to create the Datagrampacket object, at which point the byte array contains the data you want to send. In addition, the complete destination address, including the IP address and port number, is also given. The send data is implemented via the Datagramsocket Send () method, which is used to route datagrams based on the destination address of the datagram. As shown in the following code:
// Create a Datagrampacket object that sends data New datagrampacket (buf, length, address, port); // Send datagrams
When you use Datagrampacket to receive data, you feel that datagrampacket is too cumbersome to design. Developers only care about how much data the datagrampacket can put, and whether Datagrampacket uses a byte array to store the data at all. However, when Java requires the creation of a datagrampacket to receive data, an empty byte array must be passed in, and the length of the array determines how much data the datagrampacket can put, which actually exposes the implementation details of the Datagrampacket. Then Datagrampacket provides a GetData () method that returns an array of bytes encapsulated in the datagram packet object, which is more redundant-if the program needs to get an array of bytes encapsulated in Datagrampacket, Direct access to the byte array arguments passed to the Datagrampacket constructor, without calling the method.
When a Datagrampacket object is received by the server side (or the client), if you want to "feed back" some information to the sender of the datagram, but because the UDP protocol is non-connected, the receiver does not know who sent it to each datagram. However, the program can call Datagrampacket the following 3 methods to get the sender's IP address and port.
InetAddress getaddress (): When the program is ready to send this datagram, the method returns the IP address of the target machine for this datagram, and when the program has just received a datagram, the method returns the IP address of the sending host for that datagram.
int Getport (): When the program prepares to send this datagram, the method returns the port of the target machine for this datagram, and when the program has just received a datagram, the method returns the port of the sending host for that datagram.
SocketAddress getsocketaddress (): When the program is ready to send this datagram, the method returns the target socketaddress for this datagram, and when the program has just received a data chime, The method returns the socketaddress of the sending host for the datagram.
The return value of the Getsocketaddress () method is an SocketAddress object, which is actually an IP address and a port number. That is, the SocketAddress object encapsulates an InetAddress object and an integer representing the port, so using the SocketAddress object can represent both the IP address and the port.
Java uses Datagramsocket