DatagramSocket class in Java

java.net.DatagramSocket

This class represents a socket for sending and receiving datagram packets.

A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently and arrive in any order.

Where possible, a newly constructed DatagramSocket has the SO_BROADCAST socket option enabled so as to allow the transmission of broadcast datagrams.

In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address. In some implementations, broadcast packets may also be received when a DatagramSocket is bound to a more specific address.

Some methods of DatagramSocket class

DatagramSocket()This method constructs a datagram socket and binds it to any available port on the local host machine. 

DatagramSocket(int) This method constructs a datagram socket and binds it to the specified port on the local host machine.

DatagramSocket(SocketAddress)This method creates a datagram socket, bound to the specified local socket address.

DatagramSocket(int, InetAddress)This method creates a datagram socket, bound to the specified local address.

bind(SocketAddress)This method binds this DatagramSocket to a specific address and port.

connect(SocketAddress)This method connects this socket to a remote socket address (IP address + port number).

connect(InetAddress, int)This method connects the socket to a remote address for this socket. When a socket is connected to a remote address, packets may only be sent to or received from that address.

disconnect()This method disconnects the socket.

getBroadcast()This method tests if SO_BROADCAST is enabled.

getChannel()This method returns the unique java.nio.channels.DatagramChannel object associated with this datagram socket, if any.

getInetAddress()This method returns the address to which this socket is connected. Returns null if the socket is not connected.

getLocalAddress()This method gets the local address to which the socket is bound.

getLocalPort()This method returns the port number on the local host to which this socket is bound.

getLocalSocketAddress()This method returns the address of the endpoint this socket is bound to.

getOption(SocketOption)This method returns the value of a socket option.

getPort()This method returns the port number to which this socket is connected.

getReceiveBufferSize()This method gets the value of the SO_RCVBUF option for this DatagramSocket, which is the buffer size used by the platform for input on this DatagramSocket.

getRemoteSocketAddress()This method returns the address of the endpoint this socket is connected to, or null if it is unconnected.

getReuseAddress()This method tests if SO_REUSEADDR is enabled.

getSendBufferSize()This method gets the value of the SO_SNDBUF option for this DatagramSocket, which is the buffer size used by the platform for output on this DatagramSocket.

getSoTimeout()This method retrieves the setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).

getTrafficClass()This method gets the traffic class or type-of-service in the IP datagram header for packets sent from this DatagramSocket.

isBound()This method returns the binding state of the socket.

isClosed()This method returns whether the socket is closed or not.

isConnected()This method returns the connection state of the socket.

receive(DatagramPacket)This method receives a datagram packet from this socket.

send(DatagramPacket)This method sends a datagram packet from this socket.

setBroadcast(boolean)This method enables/disables SO_BROADCAST.

DatagramSocket.setDatagramSocketImplFactory(DatagramSocketImplFactory)This method sets the datagram socket implementation factory for the application.

setOption(SocketOption, String)This method sets the value of a socket option.

setReceiveBufferSize(int) This method sets the SO_RCVBUF option to the specified value for this DatagramSocket. 

setReuseAddress(boolean)This method enables/disables the SO_REUSEADDR socket option.

setSendBufferSize(int)This method sets the SO_SNDBUF option to the specified value for this DatagramSocket.

setSoTimeout(int)This method enables/disable SO_TIMEOUT with the specified timeout, in milliseconds.

setTrafficClass(int) This method sets the traffic class or type-of-service octet in the IP datagram header for datagrams sent from this DatagramSocket.

supportedOptions()This method returns a set of the socket options supported by this socket. This method will continue to return the set of options even after the socket has been closed.


Some other classes/interfaces of java.net

DatagramSocketThis class represents a socket for sending and receiving datagram packets.

DatagramSocketImplFactoryThis interface defines a factory for datagram socket implementations. It is used by the classes DatagramSocket to create actual socket implementations.

FileNameMapA simple interface that provides a mechanism to map between a file name and a MIME-type string.

HttpCookieAn HttpCookie object represents an HTTP cookie, which carries the state information between the server and the user agent. A cookie is widely adopted to create stateful sessions.

HttpRetryExceptionThis is thrown to indicate that an HTTP request needs to be retried but cannot be retried automatically, due to streaming mode being enabled.

HttpURLConnectionA URLConnection with support for HTTP-specific features.

No comments:

Post a Comment