Using Java to implement datagram communication processes

Source: Internet
Author: User
Tags socket port number

Datagram (Datagram) a logical grouping format for the transmission of information on the media by a network-layer data unit, which is an independent, self-contained message that contains address information in the network, whether it reaches the destination, the time of arrival, and the content of the arrival is not accurately known. Its communications are not required to establish a connection, for some applications that do not require high quality, datagram communication is a very good choice. In the Java java.net package, there are two classes of Datagramsocket and Datagrampacket, which are used for datagram communication in the application.

Below, I want to explain in detail in Java to implement the client-server datagram communication method, see:

The workflow of a client application

1 first to establish a datagram communication socket, we can create a Datagramsocket object to implement it, in Java Datagramsocket class has the following two constructs method:

A The public datagramsocket () constructs a datagram socket and connects it to any available port on the local host. If the socket is not open, throw the socketexception exception.

b the public datagramsocket (int port) constructs a datagram socket and connects it to the port specified by the local host. If the socket cannot be opened or the socket cannot be connected to the specified port, a SocketException exception is thrown.

2 Create a packet of packets to implement connectionless packet delivery services. Each packet is created using the Datagrampacket class, and the Datagrampacket object encapsulates the packet data, packet length, destination address, and destination port. To send a packet of data as a client, call the Datagrampacket class to create the Datagrampacket object in the form of a constructor that will send data and Baowentia address information into the object.

Datagrampacket (byte bufferedarray[],int length,inetaddress address,int port) constructs a packet length packet that is routed to a packet of packets on a specified host specified port number. Parameter length must be less than or equal to Bufferedarry.length

The Datagrampacket class provides four classes to get information

A) public byte[] GetData () returns a byte array containing the data in the datagram received or to be sent

b public int GetLength () returns the length of the data sent or received

c) Public inetaddress getaddress () returns the IP address of the machine that sent or received this datagram package

d the public int getport () returns the port number of the remote host that sent or received the datagram.

3 after creating the Datagramsocket and Datagrampacket objects, you can send a packet of data packets. The send is implemented by calling the Send method of the Datagramsocket object, which takes the Datagrampacket object as the parameter and makes the data that is encapsulated into the Datagrampacket object a datagram.

4) Of course, we can also receive packet packets, in order to receive the results returned from the server packet packets, we need to create a new Datagrampacket object, This requires another construct of Datagrampacket datagrampacket (byte bufferedarray[],int length), which simply indicates the buffer and length of the datagram that is being received. Call the Receive () method of the Datagramsocket object to complete the work of receiving datagrams, the Datagrampacket object created above needs to be used as a parameter, and the method will block until you receive a packet of packets. At this time Datagrampacket's buffer contains the received data, the packet also contains the sender's IP address, the sender's machine port number and other information.

5 processing the data in the receiving buffer to obtain the service result.

Related Article

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.