JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

Datagram Communication

Part:   1   2  3 

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

Notes and sample codes bellow are based on J2SDK 1.4.1_01.

What Is a Datagram

Datagram: An independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed. This definition is copied from the Sun's Java tutorial document. It should at least contain the following information:

  • Address: The Internet address of the remote system.
  • Port: The port number of the remote system.
  • Data: The data contained in the datagram.

Datagram Socket: A logical concept represents the contact point on the local system of a datagram communication link.

Internet Address: A unique number to identify each computer system on the Internet.

Port: A number representing an entry point of on a computer system on the Internet, where a two-way communication link can be established.

Local System: The computer system at this end of a communication link.

Remote System: The computer system at the other end of a two-way communication link.

A datagram communication link on the Internet involves:

  • Two computer systems, each has its own Internet address.
  • A port number on each computer system that is used only by this communication link during the transmission period.
  • A program on each computer system that uses this communication link. One program must act as the receiver of the datagram, and the other program must act as the sender.
  • The receiver must create a datagram socket to represent the receiving point of the communication link.
  • The sender must create a datagram socket to represent the sending point of the communication link.

The following diagram illustrates how application programs can use datagram socket send data to another computer systems through a datagram communication link on the Internet:

Computer System I                                  Computer System II
         Datagram|Address a|  Internet  |Address b|Datagram
Sender<--->Socket|Port 1   |----------->|   Port 2|Socket<--->Receiver

Establishing a Datagram Communication

To establish a datagram communication link, one application program must act as a receiver, create a datagram socket with a given local port number, and set the datagram socket in the receiving mode waiting for a datagram to arrive.

With receiver program running and waiting for a datagram to arrive, the other program can now act as a sender, create a datagram socket with a given local port number, prepare a datagram with the remote address and port number in in, and send it over.

J2SDK offers two main classes to support datagram communication:

java.net.DatagramSocket with methods:

  • DatagramSocket(): constructing a datagram socket with a given local address, and a given local port number.
  • receive(): waiting for a datagram to arrive, and returning the datagram to the caller.
  • send(): sending a datagram through the datagram socket.

java.net.DatagramPacket represeting the datagram with methods:

  • getAddress(): getting the remote address from the datagram.
  • getPort(): getting the remote port number from the datagram.
  • getData(): getting the data from the datagram.
  • setAddress(): setting the remote address to the datagram.
  • setPort(): setting the remote port to the datagram.
  • setData(): setting the data to the datagram.

(Continued on next part...)

Part:   1   2  3 

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - Datagram Communication