Datax Communication
In addition to the TCP protocol, the transmission layer of the TCP/IP protocol also has a UDP protocol. Compared with the TCP protocol, UDP is not widely used. There are several standard application layer protocols: HTTP, FTP, SMTP... TCP is used. However, UDP can be used in scenarios that require strong real-time interaction, such as online games and video conferences.
1. What is datasync?
Just like the mail system in daily life, data packets cannot be sent reliably. connection-oriented TCP is like a telephone call. Both parties are sure that the other party has received the information.
TCP is reliable and the transmission size is not limited. However, connection establishment time is required, and the error control overhead is high.
UDP is not reliable, and the error control overhead is small. The transmission size is limited to 64 KB and no connection is required.
2. Use datax
PackageJava.netProvides two classesDatagramsocketAndDatagrampacketIt is used to support datagram communication. mongoramsocket is used to establish a communication connection for transmitting datagram between programs. mongorampacket is used to represent a datagram.
Constructor of datagramsocket:
DatagramSocket ();
DatagramSocket (int prot );
DatagramSocket (int port, InetAddress laddr)
The port indicates the port number used by the socket. If the port number is not specified, connect the socket to an available port on the local host. Laddr indicates an available local address. Make sure that no port conflict occurs when the port number is given. Otherwise, exceptions of the SocketException class will be generated. Note: Both of the preceding constructor Methods declare that SocketException is not the exception during running, and must be processed in the program, captured, or declared.
When you write a client/server program in the datagram mode, you must first create an initramsocket object to receive or send a datagram, then, the initrampacket class object is used as the carrier for data transmission.
Datagrampacket constructor:
DatagramPacket (byte buf [], intlength );
DatagramPacket (byte buf [], int length, InetAddressaddr, int port );
DatagramPacket (byte [] buf, int offset, int length );
DatagramPacket (byte [] buf, int offset, int length, InetAddress address, int port );
The buf stores the datagram data. The length is the length of the data in the datagram, And the addr and port specify the destination address. The offset indicates the amount of data in the datagram.
Before receiving data, use the first method above to generate a mongorampacket object and provide the buffer and length of the received data. Then call the assumramsocket method receive () to wait for the arrival of the datagram, and receive () will wait until a datagram is received.
DatagramPacket packet = new DatagramPacket (buf, 256 );
Socket. receive (packet );
Before sending data, you need to create a new initrampacket object. In this case, you need to use the second constructor method above, while giving a buffer for storing the sent data, the complete destination address, including the IP address and port number, is also provided. Data is sent through the mongoramsocket Method send (). send () searches for the path based on the destination address of the datagram to transmit the datagram.
Datagrampacket packet = new datagrampacket (BUF, length, address, Port );
Socket. Send (packet );
When constructing a Data Report, The inetaddress class parameter is required. The class inetaddress is defined in the java.net package to indicate an Internet address. We can use the class method getbyname () provided by the package to obtain the IP address of the host from a string that represents the host name, then obtain the corresponding address information.
3. Use dataphin for broadcasting (multicastsocket)
Datagramsocket only allows a datagram to send a destination address. The java.net package provides a multicastsocket class that allows data to be broadcast to all customers on the port. Multicastsocket is used on the client to listen to data broadcast by the server.