Table 1: Key Commonly Used Methods of the DatagramSocket API (adapted from [3])
No.
Constructor/ Method Description
DatagramSocket class
1 DatagramSocket( )
Constructs an object of class DatagramSocket and binds the
object to any available port on the local host machine
2 DatagramSocket(int port)
Constructs an object of class DatagramSocket and binds it to
the specified port on the local host machine
3
DatagramSocket (int port, InetAddress
addr)
Constructs an object of class DatagramSocket and binds it to
the specified local address and port
4 void close( ) Closes the datagram socket
5
void connect(InetAddress address, int
port)
Connects the datagram socket to the specified remote address
and port number on the machine with that address
6 InetAddress getLocalAddress( )
Returns the local InetAddress to which the socket is
connected.
7 int getLocalPort( )
Returns the port number on the local host to which the
datagram socket is bound
8
InetAddress getInetAddress( ) Returns the IP address to which the datagram socket is
connected to at the remote side.
9 int getPort( ) Returns the port number at the remote side of the socket
10 void receive(DatagramPacket packet) Receives a datagram packet object from this socket
11 void send(DatagramPacket packet) Sends a datagram packet object from this socket
12 void setSoTimeout(int timeout) Set the timeout value for the socket, in milliseconds
DatagramPacket class
13
DatagramPacket(byte[ ] buf, int length,
InetAddress, int port)
Constructs a datagram packet object with the contents stored
in a byte array, buf, of specified length to a machine with the
specified IP address and port number
14 InetAddress getAddress( )
Returns the IP address of the machine at the remote side to
which the datagram is being sent or from which the datagram
was received
15 byte [ ] getData( ) Returns the data buffer stored in the packet as a byte array
16 int getLength( )
Returns the length of the data buffer in the datagram packet
sent or received
17 int getPort( )
Returns the port number to which the datagram socket is
bound to which the datagram is being sent or from which the
datagram is received
18 void setData(byte [ ]) Sets the data buffer for the datagram packet
19 void setAddress(InetAddress iaddr)
Sets the datagram packet with the IP address of the remote
machine to which the packet is being sent
20 void setPort(int port )
Sets the datagram packet with the port number of the
datagram socket at the remote host to which the packet is sent
With connectionless sockets, a DatagramSocket object bound to a process can be used to send
datagrams to different destinations. Also, multiple processes can simultaneously send datagrams
to the same socket bound to a receiving process. In such a situation, the order of the arrival of the
datagrams may not be consistent with the order they were sent from the different processes. Note
that in connection-oriented or connectionless Socket APIs, the send operations are non-blocking
and the receive operations are blocking. A process continues its execution after the issuance of a
send method call. On the other hand, once a process calls the receive method on a socket, the
process is suspended until a datagram is received. To avoid indefinite blocking, the setSoTimeout
method can be called on the DatagramSocket object.