A survey of Java datagram programming

Source: Internet
Author: User
Tags object bind constructor header socket port number
Programming | data

General description

In the TCP/IP protocol family, UDP and TCP are also located in the transport layer, and the user datagram is the concept in the UDP protocol.

The UDP protocol provides a simple, transaction-oriented, unreliable information delivery service that does not provide a reliable mechanism for IP protocols, flow control, and error recovery functions.

UDP protocol is basically the interface between IP protocol and upper layer protocol, from the whole user data in the packaging of all layers, the UDP message format is quite simple:

1632bitSource Port Source Ports Destination port length message length (in bytes, including header and user data area) Checksum (checksum) data


Because of the checksum, UDP also introduces pseudo header, which causes the UDP and IP layer to be too close, which destroys the layering principle.

Java Datagram Support

The packet java.net provides two classes Datagramsocket and Datagrampacket to support datagram Communications, datagramsocket for communication connections between programs to establish transmission datagrams, Datagrampacket is used to represent a datagram.

Datagramsocket represents a socket for sending and receiving datagrams, and a datagram socket is the sending and receiving point for the package delivery service. On a datagram socket, each packet sent and received is individually addressed and routed, and multiple packets sent from one machine to another are routed differently. Any order of arrival.

It is always possible to send a DATAGRAMSOCKET,UDP broadcast (that is the default). To receive broadcast packets this class instance should be bound to a generic address (wildcard addresses). In some implementations, broadcast packets can also be received when they are bound to more specific addresses.

For example:

Datagramsocket s = new datagramsocket (null);
S.bind (New inetsocketaddress (8888));
This equates to:
Datagramsocket s = new Datagramsocket (8888);  
Two scenarios Create a Datagramsocket instance that can receive broadcasts on port 8888.

Constructor Summary datagramsocket ()
           Constructs a datagram socket and binds it to all available port on the local host machine.protected Datagramsocket (Datagra Msocketimpl impl)
          creates an unbound datagram Socket with the specified Datagramsocketimpl. datagramsocket (int port)
           constructs a datagram socket and binds it to the specified port on the local host MACHINE.&N Bsp;datagramsocket (Int port, inetaddress laddr)
           creates a datagram socket, bound to the specified local address. datagramsocket (socketaddress  BINDADDR)
          creates A datagram socket, bound to the Specified local socket address. 

Where port indicates the port number used by the socket, and if the port number is not indicated, connect the socket to the available port on the local host. LADDR indicates an available local address. To ensure that no port conflicts occur when the port number is given, the SocketException class exception is generated.

When writing a communication program by means of a datagram, both parties must first establish a Datagramsocket object to receive or send datagrams, and then use the Datagrampacket class object as the carrier for transmitting data. Let's look at how Datagrampacket is constructed:

Constructor Summarydatagrampacket (byte[] buf, int length)
Constructs a datagrampacket for receiving packets of length length. Datagrampacket (byte[] buf, int length, inetaddress address, int port)
Constructs a datagram packet for sending packets ' length length to the ' specified port number on the specified host. Datagrampacket (byte[] buf, int offset, int length)
Constructs a datagrampacket for receiving packets of length length, and specifying a offset into the buffer. Datagrampacket (byte[] buf, int offset, int length, inetaddress address, int port)
Constructs a datagram packet for sending packets ' length length with offset Ioffsetto ' specified port number on the ' s Pecified host. Datagrampacket (byte[] buf, int offset, int length, socketaddress address)
Constructs a datagram packet for sending packets ' length length with offset Ioffsetto ' specified port number on the ' s Pecified host. Datagrampacket (byte[] buf, int length, socketaddress address)
Constructs a datagram packet for sending packets ' length length to the ' specified port number on the specified host.

As you can see, there are two constructors to receive and four constructors to send. Among them, the data in the buf, length for the data in the datagram, address and port aim for the location of eyesight, offset indicates the displacement of the datagram.

Java Multicast support

MulticastSocket Multicast datagram sockets. This multicast socket is useful for sending and receiving IP multicast packets, extending the datagramsocket and attaching a way to join multicast groups on the Internet. A multicast group is specified by Class D IP addresses and standard UDP ports, and Class D IP ranges are 224.0.0.0 to 239.255.255.255, where 224.0.0.0 is reserved for it.

It has three constructors:

Constructor Summarymulticastsocket ()
Create a multicast socket. MulticastSocket (int port)
Create a multicast socket and bind it to a specific port. MulticastSocket (socketaddress bindaddr)
Create a multicastsocket bound to the specified socket address.

Basically, there is no specified port, only for sending, the specified port can be sent and received, the multiple access host will use the socket address.




Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.