java.net
Class DatagramSocket

java.lang.Object
  extended by java.net.DatagramSocket
Direct Known Subclasses:
MulticastSocket

public class DatagramSocket
extends Object

This class models a connectionless datagram socket that sends individual packets of data across the network. In the TCP/IP world, this means UDP. Datagram packets do not have guaranteed delivery, or any guarantee about the order the data will be received on the remote host.


Constructor Summary
  DatagramSocket()
          Initializes a new instance of DatagramSocket that binds to a random port and every address on the local machine.
protected DatagramSocket(DatagramSocketImpl impl)
          Creates a DatagramSocket from a specified DatagramSocketImpl instance
  DatagramSocket(int port)
          Initializes a new instance of DatagramSocket that binds to the specified port and every address on the local machine.
  DatagramSocket(int port, InetAddress addr)
          Initializes a new instance of DatagramSocket that binds to the specified local port and address.
  DatagramSocket(SocketAddress address)
          Initializes a new instance of DatagramSocket that binds to the specified local port and address.
 
Method Summary
 void bind(SocketAddress address)
          Binds the socket to the given socket address.
 void close()
          Closes this datagram socket.
 void connect(InetAddress address, int port)
          This method connects this socket to the specified address and port.
 void connect(SocketAddress address)
          Connects the datagram socket to a specified socket address.
 void disconnect()
          This method disconnects this socket from the address/port it was connected to.
 boolean getBroadcast()
          Checks if SO_BROADCAST is enabled
 DatagramChannel getChannel()
          Returns the datagram channel assoziated with this datagram socket.
 InetAddress getInetAddress()
          This method returns the remote address to which this socket is connected.
 InetAddress getLocalAddress()
          Returns the local address this datagram socket is bound to.
 int getLocalPort()
          Returns the local port this socket is bound to.
 SocketAddress getLocalSocketAddress()
          Returns the local SocketAddress this socket is bound to.
 int getPort()
          This method returns the remote port to which this socket is connected.
 int getReceiveBufferSize()
          This method returns the value of the system level socket option SO_RCVBUF, which is used by the operating system to tune buffer sizes for data transfers.
 SocketAddress getRemoteSocketAddress()
          Returns the SocketAddress of the host this socket is conneted to or null if this socket is not connected.
 boolean getReuseAddress()
          Checks if SO_REUSEADDR is enabled.
 int getSendBufferSize()
          This method returns the value of the system level socket option SO_SNDBUF, which is used by the operating system to tune buffer sizes for data transfers.
 int getSoTimeout()
          Returns the value of the socket's SO_TIMEOUT setting.
 int getTrafficClass()
          Returns the current traffic class
 boolean isBound()
          Returns the binding state of the socket.
 boolean isClosed()
          Checks if the datagram socket is closed.
 boolean isConnected()
          Returns the connection state of the socket.
 void receive(DatagramPacket p)
          Reads a datagram packet from the socket.
 void send(DatagramPacket p)
          Sends the specified packet.
 void setBroadcast(boolean enable)
          Enables/Disables SO_BROADCAST
static void setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
          Sets the datagram socket implementation factory for the application
 void setReceiveBufferSize(int size)
          This method sets the value for the system level socket option SO_RCVBUF to the specified value.
 void setReuseAddress(boolean on)
          Enables/Disables SO_REUSEADDR.
 void setSendBufferSize(int size)
          This method sets the value for the system level socket option SO_SNDBUF to the specified value.
 void setSoTimeout(int timeout)
          Sets the value of the socket's SO_TIMEOUT value.
 void setTrafficClass(int tc)
          Sets the traffic class value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatagramSocket

protected DatagramSocket(DatagramSocketImpl impl)
Creates a DatagramSocket from a specified DatagramSocketImpl instance

Parameters:
impl - The DatagramSocketImpl the socket will be created from
Since:
1.4

DatagramSocket

public DatagramSocket()
               throws SocketException
Initializes a new instance of DatagramSocket that binds to a random port and every address on the local machine.

Throws:
SocketException - If an error occurs.
SecurityException - If a security manager exists and its checkListen method doesn't allow the operation.

DatagramSocket

public DatagramSocket(int port)
               throws SocketException
Initializes a new instance of DatagramSocket that binds to the specified port and every address on the local machine.

Parameters:
port - The local port number to bind to.
Throws:
SecurityException - If a security manager exists and its checkListen method doesn't allow the operation.
SocketException - If an error occurs.

DatagramSocket

public DatagramSocket(int port,
                      InetAddress addr)
               throws SocketException
Initializes a new instance of DatagramSocket that binds to the specified local port and address.

Parameters:
port - The local port number to bind to.
addr - The local address to bind to.
Throws:
SecurityException - If a security manager exists and its checkListen method doesn't allow the operation.
SocketException - If an error occurs.

DatagramSocket

public DatagramSocket(SocketAddress address)
               throws SocketException
Initializes a new instance of DatagramSocket that binds to the specified local port and address.

Parameters:
address - The local address and port number to bind to.
Throws:
SecurityException - If a security manager exists and its checkListen method doesn't allow the operation.
SocketException - If an error occurs.
Since:
1.4
Method Detail

close

public void close()
Closes this datagram socket.


getInetAddress

public InetAddress getInetAddress()
This method returns the remote address to which this socket is connected. If this socket is not connected, then this method will return null.

Returns:
The remote address.
Since:
1.2

getPort

public int getPort()
This method returns the remote port to which this socket is connected. If this socket is not connected, then this method will return -1.

Returns:
The remote port.
Since:
1.2

getLocalAddress

public InetAddress getLocalAddress()
Returns the local address this datagram socket is bound to.

Returns:
The local address is the socket is bound or null
Since:
1.1

getLocalPort

public int getLocalPort()
Returns the local port this socket is bound to.

Returns:
The local port number.

getSoTimeout

public int getSoTimeout()
                 throws SocketException
Returns the value of the socket's SO_TIMEOUT setting. If this method returns 0 then SO_TIMEOUT is disabled.

Returns:
The current timeout in milliseconds.
Throws:
SocketException - If an error occurs.
Since:
1.1

setSoTimeout

public void setSoTimeout(int timeout)
                  throws SocketException
Sets the value of the socket's SO_TIMEOUT value. A value of 0 will disable SO_TIMEOUT. Any other value is the number of milliseconds a socket read/write will block before timing out.

Parameters:
timeout - The new SO_TIMEOUT value in milliseconds.
Throws:
SocketException - If an error occurs.
Since:
1.1

getSendBufferSize

public int getSendBufferSize()
                      throws SocketException
This method returns the value of the system level socket option SO_SNDBUF, which is used by the operating system to tune buffer sizes for data transfers.

Returns:
The send buffer size.
Throws:
SocketException - If an error occurs.
Since:
1.2

setSendBufferSize

public void setSendBufferSize(int size)
                       throws SocketException
This method sets the value for the system level socket option SO_SNDBUF to the specified value. Note that valid values for this option are specific to a given operating system.

Parameters:
size - The new send buffer size.
Throws:
SocketException - If an error occurs.
IllegalArgumentException - If size is 0 or negative.
Since:
1.2

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws SocketException
This method returns the value of the system level socket option SO_RCVBUF, which is used by the operating system to tune buffer sizes for data transfers.

Returns:
The receive buffer size.
Throws:
SocketException - If an error occurs.
Since:
1.2

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws SocketException
This method sets the value for the system level socket option SO_RCVBUF to the specified value. Note that valid values for this option are specific to a given operating system.

Parameters:
size - The new receive buffer size.
Throws:
SocketException - If an error occurs.
IllegalArgumentException - If size is 0 or negative.
Since:
1.2

connect

public void connect(InetAddress address,
                    int port)
This method connects this socket to the specified address and port. When a datagram socket is connected, it will only send or receive packets to and from the host to which it is connected. A multicast socket that is connected may only send and not receive packets.

Parameters:
address - The address to connect this socket to.
port - The port to connect this socket to.
Throws:
IllegalArgumentException - If address or port are invalid.
SecurityException - If the caller is not allowed to send datagrams to or receive from this address and port.
Since:
1.2

disconnect

public void disconnect()
This method disconnects this socket from the address/port it was connected to. If the socket was not connected in the first place, this method does nothing.

Since:
1.2

receive

public void receive(DatagramPacket p)
             throws IOException
Reads a datagram packet from the socket. Note that this method will block until a packet is received from the network. On return, the passed in DatagramPacket is populated with the data received and all the other information about the packet.

Parameters:
p - A DatagramPacket for storing the data
Throws:
IOException - If an error occurs.
SocketTimeoutException - If setSoTimeout was previously called and the timeout has expired.
PortUnreachableException - If the socket is connected to a currently unreachable destination. Note, there is no guarantee that the exception will be thrown.
IllegalBlockingModeException - If this socket has an associated channel, and the channel is in non-blocking mode.
SecurityException - If a security manager exists and its checkAccept method doesn't allow the receive.

send

public void send(DatagramPacket p)
          throws IOException
Sends the specified packet. The host and port to which the packet are to be sent should be set inside the packet.

Parameters:
p - The datagram packet to send.
Throws:
IOException - If an error occurs.
SecurityException - If a security manager exists and its checkMulticast or checkConnect method doesn't allow the send.
PortUnreachableException - If the socket is connected to a currently unreachable destination. Note, there is no guarantee that the exception will be thrown.
IllegalBlockingModeException - If this socket has an associated channel, and the channel is in non-blocking mode.

bind

public void bind(SocketAddress address)
          throws SocketException
Binds the socket to the given socket address.

Parameters:
address - The socket address to bind to.
Throws:
SocketException - If an error occurs.
SecurityException - If a security manager exists and its checkListen method doesn't allow the operation.
IllegalArgumentException - If address type is not supported.
Since:
1.4

isClosed

public boolean isClosed()
Checks if the datagram socket is closed.

Returns:
True if socket is closed, false otherwise.
Since:
1.4

getChannel

public DatagramChannel getChannel()
Returns the datagram channel assoziated with this datagram socket.

Returns:
The associated DatagramChannel object or null
Since:
1.4

connect

public void connect(SocketAddress address)
             throws SocketException
Connects the datagram socket to a specified socket address.

Parameters:
address - The socket address to connect to.
Throws:
SocketException - If an error occurs.
IllegalArgumentException - If address type is not supported.
Since:
1.4

isBound

public boolean isBound()
Returns the binding state of the socket.

Returns:
True if socket bound, false otherwise.
Since:
1.4

isConnected

public boolean isConnected()
Returns the connection state of the socket.

Returns:
True if socket is connected, false otherwise.
Since:
1.4

getRemoteSocketAddress

public SocketAddress getRemoteSocketAddress()
Returns the SocketAddress of the host this socket is conneted to or null if this socket is not connected.

Returns:
The socket address of the remote host if connected or null
Since:
1.4

getLocalSocketAddress

public SocketAddress getLocalSocketAddress()
Returns the local SocketAddress this socket is bound to.

Returns:
The local SocketAddress or null if the socket is not bound.
Since:
1.4

setReuseAddress

public void setReuseAddress(boolean on)
                     throws SocketException
Enables/Disables SO_REUSEADDR.

Parameters:
on - Whether or not to have SO_REUSEADDR turned on.
Throws:
SocketException - If an error occurs.
Since:
1.4

getReuseAddress

public boolean getReuseAddress()
                        throws SocketException
Checks if SO_REUSEADDR is enabled.

Returns:
True if SO_REUSEADDR is set on the socket, false otherwise.
Throws:
SocketException - If an error occurs.
Since:
1.4

setBroadcast

public void setBroadcast(boolean enable)
                  throws SocketException
Enables/Disables SO_BROADCAST

Parameters:
enable - True if SO_BROADCAST should be enabled, false otherwise.
Throws:
SocketException - If an error occurs
Since:
1.4

getBroadcast

public boolean getBroadcast()
                     throws SocketException
Checks if SO_BROADCAST is enabled

Returns:
Whether SO_BROADCAST is set
Throws:
SocketException - If an error occurs
Since:
1.4

setTrafficClass

public void setTrafficClass(int tc)
                     throws SocketException
Sets the traffic class value

Parameters:
tc - The traffic class
Throws:
SocketException - If an error occurs
IllegalArgumentException - If tc value is illegal
Since:
1.4
See Also:
getTrafficClass()

getTrafficClass

public int getTrafficClass()
                    throws SocketException
Returns the current traffic class

Returns:
The current traffic class.
Throws:
SocketException - If an error occurs
Since:
1.4
See Also:
setTrafficClass(int tc)

setDatagramSocketImplFactory

public static void setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
                                         throws IOException
Sets the datagram socket implementation factory for the application

Parameters:
fac - The factory to set
Throws:
IOException - If an error occurs
SocketException - If the factory is already defined
SecurityException - If a security manager exists and its checkSetFactory method doesn't allow the operation