Creates a datagram socket and binds it to any available port on the local host machine. This socket is also bound to all IP addresses. This means that the socket may be referred to by any IP address of the computer.
protected DatagramSocket(DatagramSocketImpl impl)
Constructs a datagram socket with the specified DatagramSocketImpl object. This socket is not yet bound to any address or port.
Creates a datagram socket and binds it to the specified port on the local computer. This socket is also bound to all IP addresses. This means that the socket may be referred to by any IP address of the computer.
DatagramSocket(int port, InetAddress address)
Creates a datagram socket, bound to the specified port and address. If the port is 0 (zero), an ephemeral port is used. If the address is 0.0.0.0, socket is bound to all addresses.
Creates a datagram socket and bounds to the specified local socket address.
void bind(SocketAddress addr)
Binds the DatagramSocket object to the specified address (IP address and port). This method expects address as InetSocketAddress object. The InetSocketAddress class extends SocketAddress class and implements an IP Socket Address as [IP address, port number] or [hostname, port number] pair. In the latter case, an attempt will be made to resolve the hostname. If resolution fails then the address is said to be unresolved and a SocketException is thrown. However, the name can still be used on some circumstances such as connecting through a proxy. If the address is null, bind() picks up an ephemeral port and a valid local address to bind the socket.
void close ()
Closes this datagram socket. If a thread is currently blocked on received method, then the thread throws a SocketException.
void connect(InetAddress address, int port)
The functionality of this method is significantly different from the connect() method of Socket class. Connects the socket to a remote socket having specified address and port. Usually, a datagram socket (call it receiver) receives data using received method from any other datagram socket (call it sender). If a receiver wants to receive data from a particular sender, it uses connect() method specifying the address and port of the sender socket. Subsequent received method calls reject data sent by other sockets. The connect() method may also be used to choose a particular receiver among a group of receivers identified by a multicast address (e.g. 22.214.171.124). All hosts in a multicast group are identified by a single multicast address. If a sender wants to send data to a particular host in this group, the sender may use connect() method specifying the receivers’ own address and port (which is used in multicast socket address). The sender must also use the same address and port in the datagram packet, otherwise an IllegalArgumentException will be thrown.
void connect(SocketAddress addr)
Same as the previous one except the argument is a SocketAddress object that represents the address and port. Actually an InetSocketAddress object that represents IP address and port is passed to this method.
void disconnect ()
Disconnects the socket. This method has no effect if the socket is not connected or already closed.
boolean getBroadcast ()
Returns true if so_broadcast is enabled, false otherwise.
DatagramChannel getChannel ()
Returns the unique DatagramChannel object associated with this datagram socket, if any.
Returns the address to which this socket is connected. If the socket is not connected it returns null.
Returns the local address to which the socket is bound.
Returns the port number on the local host to which this socket is bound.
Returns the socket address (address and port) of the endpoint this socket is bound to.
int getPort ()
Returns the port number to which this socket is connected. If the socket is not connected, it returns -1.
Get value of the so_rcvbuf option for this DatagramSocket, that is the buffer size used by the platform for input on this DatagramSocket.
Returns the socket address (address and port) of the endpoint this socket is connected to, or null if the socket is not connected.
Tests if so reuseaddr is enabled.
int getSendBufferSize ()
Get value of the so_sndbuf option for this DatagramSocket, that is the buffer size used by the platform for output on this DatagramSocket.
Returns SocketOptions.so_TIMEOUT value. The value 0 indicates that timeout option is disabled (i.e. infinite timeout).
int getTrafficClass ()
Returns traffic class or Type-of-Service (ToS) in the IP datagram header for packets sent from this
DatagramSocket. boolean isBound()
Returns true if the socket is successfully bound to a socket address, false otherwise.
Returns true if the socket is closed, false otherwise.
boolean isConnected ()
Returns true if the socket is connected to a remote socket, false otherwise.
void receive(DatagramPacket p)
Receives a DatagramPacket if the packet is intended (i.e. the destination address of the packet matches socket’s address) for this socket or the packet is a broadcast one. This method blocks until a datagram is received. When this method returns, the DatagramPacket’s buffer is filled with the data received. The packet also contains the IP address and the port number of the sender DatagramSocket. The length field of the datagram packet object contains the length of the received message. If the message is longer than the buffer’s size, the message is truncated.
void send(DatagramPacket p)
Sends a DatagramPacket from this socket. The packet contains socket address (IP address and port number) of the intended destination host together with the data (payload). Note that the IP address may be a unicast or multicast or broadcast address. It indicates that a simple DatagramSocket may be used for unicasting, broadcasting and multicasting of packets.
void setBroadcast(boolean on)
Sets SocketOptions.o_BROADCAST option of the socket to true/false that enables/disables the ability of the process to send broadcast messages through datagram sockets. It works only on networks that support the concept of a broadcast message (e.g. Ethernet, token ring, etc.). The default value of this option is true.
static void setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
Sets the datagram socket implementation factory for the application.
void setReceiveBufferSize(int size)
Sets the so_rcvbuf option for this DatagramSocket to the specified size. This option is used to give a hint to the size of the underlying network buffer the kernel should use for receiving datagrams. A larger value of this option may allow the network implementation to buffer multiple packets when packets arrive faster than are being received using receive() method.
void setReuseAddress(boolean on)
Enable/disable the SO_REUSEADDR socket option.
void setSendBufferSize(int size)
Sets the so_sndbuf option for this DatagramSocket to the specified size. This option is used to give a hint to the size of the underlying network buffer the kernel should use for sending datagrams. A larger value of this option may allow the network implementation to queue multiple packets when packets are submitted faster than are being sent using send() method.
Sets the SO_SNDBUF option to the specified value for this DatagramSocket.
void setSoTimeout(int timeout)
Sets the SocketOptions.so_TiMEOUT value with the specified timeout in milliseconds. If a nonzero timeout value is specified and subsequently receive() method is called on this datagram socket, the receive() method waits for only a specified amount of time for incoming data. If data comes within this specified time, a SocketTimeoutException is thrown. However, the datagram socket remains valid and a different timeout value may be used (if required) later and receive)) method may also be called subsequently to have its effect. The timeout value is negative, an iiiegaiArgumentException is raised. A timeout value of 0 (which is also the default timeout) is used for infinite timeout.
void setTrafficClass(int tc)
Note that Type-of-Service (TOS) is an 8-bit field in the IP datagram header. This field indicates the abstract parameters of the quality of service desired. The setTrafficCiass() method is used to set this field with the specified tc for datagrams sent from this DatagramSocket. However, the underlying network layer may ignore this. The value of tc must be in the range 0 to 255, otherwise an IiiegaiArgumentException is thrown. A summary of the TOS field is given below:
Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.