Java DatagramSocket and DatagramPacket: UDP Connection-Less Socket Programming
Explore the DatagramSocket and DatagramPacket classes in Java, essential for connection-less socket programming using the UDP protocol. Learn how datagrams facilitate data transmission across networks without guaranteed delivery, offering a lightweight alternative to TCP in Java networking.
Java DatagramSocket and DatagramPacket
The Java DatagramSocket and DatagramPacket classes are used for connection-less socket programming using UDP instead of TCP.
Datagram
Datagrams are collections of information sent from one device to another via a network. There is no assurance that the datagram will reach the target device safely or completely. UDP protocol implements datagrams in Java.
Java DatagramSocket Class
Java DatagramSocket class represents a connection-less socket for sending and receiving datagram packets.
Commonly used constructors:
- DatagramSocket() throws SocketException: Creates a datagram socket and binds it with the available Port Number on the localhost machine.
- DatagramSocket(int port) throws SocketException: Creates a datagram socket and binds it with the given Port Number.
- DatagramSocket(int port, InetAddress address) throws SocketException: Creates a datagram socket and binds it with the specified port number and host address.
Java DatagramSocket Class Methods
Method | Description |
---|---|
void bind(SocketAddress addr) | Binds the DatagramSocket to a specific address and port. |
void close() | Closes the datagram socket. |
void connect(InetAddress address, int port) | Connects the socket to a remote address. |
void disconnect() | Disconnects the socket. |
boolean getBroadcast() | Tests if SO_BROADCAST is enabled. |
DatagramChannel getChannel() | Returns the unique DatagramChannel object associated with the socket. |
InetAddress getInetAddress() | Returns the address to which the socket is connected. |
InetAddress getLocalAddress() | Gets the local address to which the socket is connected. |
int getLocalPort() | Returns the port number on the local host to which the socket is bound. |
SocketAddress getLocalSocketAddress() | Returns the address of the endpoint the socket is bound to. |
int getPort() | Returns the port number to which the socket is connected. |
int getReceiverBufferSize() | Gets the value of the SO_RCVBUF option for this DatagramSocket. |
boolean isClosed() | Returns the status of the socket. |
boolean isConnected() | Returns the connection state of the socket. |
void send(DatagramPacket p) | Sends the datagram packet from the socket. |
void receive(DatagramPacket p) | Receives the datagram packet from the socket. |
Java DatagramPacket Class
Java DatagramPacket is a message that can be sent or received. It is a data container, and if multiple packets are sent, they may arrive in any order. Packet delivery is not guaranteed.
Commonly used constructors:
- DatagramPacket(byte[] barr, int length): Creates a datagram packet for receiving packets.
- DatagramPacket(byte[] barr, int length, InetAddress address, int port): Creates a datagram packet for sending packets.
Java DatagramPacket Class Methods
Method | Description |
---|---|
InetAddress getAddress() | Returns the IP address of the machine to which the datagram is being sent or from which it was received. |
byte[] getData() | Returns the data buffer. |
int getLength() | Returns the length of the data to be sent or received. |
int getOffset() | Returns the offset of the data to be sent or received. |
int getPort() | Returns the port number on the remote host to which the datagram is being sent or from which it was received. |
SocketAddress getSocketAddress() | Gets the SocketAddress (IP address + port number) of the remote host. |
void setAddress(InetAddress iaddr) | Sets the IP address of the machine to which the datagram is being sent. |
void setData(byte[] buff) | Sets the data buffer for the packet. |
void setLength(int length) | Sets the length of the packet. |
void setPort(int iport) | Sets the port number on the remote host to which the datagram is being sent. |
void setSocketAddress(SocketAddress addr) | Sets the SocketAddress (IP address + port number) of the remote host. |
Example of Sending DatagramPacket by DatagramSocket
Syntax
import java.net.*;
public class DSender {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String str = "Welcome java";
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
ds.send(dp);
ds.close();
}
}
Output
DatagramSocket and DatagramPacket
Example of Receiving DatagramPacket by DatagramSocket
Syntax
import java.net.*;
public class DReceiver {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(str);
ds.close();
}
}
Output
Welcome java