Class DatagramSocket in Java

Class DatagramSocket in Java

DatagramSocket class represents a socket for sending and receiving datagram unreliable packets using the UDP protocol. A datagram socket is the sending or receiving point for a connectionless packet delivery service.

Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from a machine to another may be routed differently and may arrive in any order.

Ads code goes here

A datagram is a very low-level networking interface: it is simply an array of bytes sent over the network. A datagram does not implement any kind of stream-based communication protocol, and there is no connection established between the sender and the receiver.

Datagram packets are called “unreliable” because the protocol does not make any attempt to ensure that they arrived or to resend them if they did not. Thus, packets sent through a DatagramSocket are not guaranteed to arrive in the order sent or to arrive at all. On the other hand, this low-overhead protocol makes datagram transmission very fast.

The DatagramSocket class implements packet-oriented, connectionless data communication. In Internet parlance, this is the User Datagram Protocol, commonly known as UDP. Each packet wanders through the network, routed by its destination address. Different packets can take different paths through the network and may arrive in a different order than they were sent.

Furthermore, packets are not even guaranteed to reach their destination. It is up to an application that uses DatagramSocket to determine if data is out of order or missing. While these features may seem like disadvantages of DatagramSocket, there is also some advantage to using this class. Primarily, communication using DatagramSocket is faster than
Socket stream communication because of the lack of overhead involved.

READ  Abstract Class Calendar in Java

If a port is specified when the DatagramSocket is created, that port is used; otherwise, the system assigns a port.

getLocalPort() returns the port number in use.

send() sends a DatagramPacket through the socket. The packet must contain the destination address to which it should be sent.

receive() waits for data to arrive at the socket and stores it, along with the address of the sender, into the specified DatagramPacket.

close() closes the socket and frees the port it used for reuse. Once close() has been called, the DatagramSocket should not be used again.

The class DatagramSocket is given as:

public class java.net.DatagramSocket extends java.lang.Object{
//constructors
public DatagramSocket()throws SocketException;
//constructs a DatagramSocket and binds it to any available port on the localhost machine It may throw SocketException if the socket could not be opened or the socket could not be bind to the specified local port. This is used to make communication between two ports in the local machines 
public DatagramSocket(int port, InetAddress laddr) throws SocketException;
public DatagramSocket(int port)throws SocketException;//constructs a DatagramSocket and binds it to any available specified port on the localhost machine It may throw SocketException if the socket could not be opened or the socket could not be bind to the specified local port. This constructor is used to communicate between two nonlocalized ports.
//Methods:
public void close();//To close the datagram socket that is established when the communication is completed
public InetAddress getLocalAddress();
public synchronized int getSoTimeout() throws SocketException;
public int getLocalPort();
public synchronized void receive(DatagramPacket pck)throws IOException;//takes the  Datagram packet object as a recipient for the datagram to be received.
public void send(DatagramPacket pck)throws IOException;//Takes a Datagram packet object and sends the datagram's data to the previously defined host and port address.
public synchronized void setSoTimeout(int timeout) throws SocketException;
} 

The details of the class structure are given as follows:

public DatagramSocket();

public DatagramSocket() constructor creates a DatagramSocket that is bound to any available port on the local host
machine.

public DatagramSocket(int port);

public DatagramSocket(int port) constructor creates a DatagramSocket that is bound to the given port on the local host machine.

READ  JSoup A Nice Initiative To Fetch Information From HTML

Parameter
port – A port number.

public DatagramSocket(int port, InetAddress laddr); 

public DatagramSocket(int port, InetAddress laddr); constructor creates a DatagramSocket that is bound to the given port on the specified local host machine.

Parameter
port – A port number.
laddr – A local address.

public void close();

public void close() method closes the socket, releasing any system resources it holds.

public InetAddress getLocalAddress();

public InetAddress getLocalAddress() method returns the local address to which this DatagramSocket is bound.

This method returns the local address of the socket.

public int getLocalPort();

public int getLocalPort() method returns the local port to which this DatagramSocket is bound.

This method returns the port number of the socket.

public synchronized int getSoTimeout();

public synchronized int getSoTimeout() method returns the receive time-out value for this socket. A value of zero indicates that the socket waits indefinitely for an incoming packet, while a non-zero value indicates the number of milliseconds it waits.

This method returns the receive time-out value for the socket.

public synchronized void receive(DatagramPacket p);

public synchronized void receive(DatagramPacket p) method receives a datagram packet on this socket. After this method returns, the given DatagramPacket contains the packet’s data and length, and the sender’s address and port number.

If the data that was sent is longer than the given packet’s data buffer, the data is truncated.
If a time-out value is specified using the setSoTimeout() method, the method either returns with the received packet or times out, throwing an InterruptedIOException. If no time-out value is specified, the method blocks until it receives a packet.

Parameter
p – The DatagramPacket that receives incoming data.

public void send(DatagramPacket p);

public void send(DatagramPacket p) method sends a packet from this socket. The packet data, packet length, destination address, and destination port number are specified by the given DatagramPacket.

READ  Class Date in Java

Parameter
p – The DatagramPacket to be sent.

public synchronized void setSoTimeout(int timeout);

public synchronized void setSoTimeout(int timeout) method is used to set the time-out value that is used to receive(). A non-zero value specifies the length of time, in milliseconds, that the DatagramSocket should wait for an incoming packet.

A value of zero indicates that the DatagramSocket should wait indefinitely for an incoming packet. If a time-out value is needed, this method must be called before receive().

Parameter
timeout – The new time-out value, in milliseconds, for this socket.

Apart from these DatagramSocket class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *