p2ps.imp.net.inet
Class InetDatagramSocket

java.lang.Object
  extended byp2ps.imp.net.inet.InetDatagramSocket
All Implemented Interfaces:
DatagramSocket
Direct Known Subclasses:
InetMulticastSocket

public class InetDatagramSocket
extends java.lang.Object
implements DatagramSocket

A datagram socket implementation that binds to java.net.DatagramSocket

Version:
$Revision: 295 $
Author:
Ian Wang

Constructor Summary
InetDatagramSocket(java.net.DatagramSocket socket)
           
 
Method Summary
 void bind(java.net.SocketAddress addr)
          Binds this DatagramSocket to a specific address & port.
 void close()
          Closes this datagram socket.
 void connect(java.net.SocketAddress addr)
          Connects this socket to a remote socket address (IP address + port number).
 void disconnect()
          Disconnects the socket.
 int getLocalPort()
          Returns the port number on the local host to which this socket is bound.
 java.net.SocketAddress getLocalSocketAddress()
          Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.
 int getPort()
          Returns the port for this socket.
 int getReceiveBufferSize()
          Get value of the SO_RCVBUF option for this DatagramSocket, that is the buffer size used by the platform for input on this DatagramSocket.
 java.net.SocketAddress getRemoteSocketAddress()
          Returns the address of the endpoint this socket is connected to, or null if it is unconnected.
 boolean getReuseAddress()
          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.
 int getSoTimeout()
          Retrive setting for SO_TIMEOUT.
 boolean isBound()
          Returns the binding state of the socket.
 boolean isClosed()
          Returns whether the socket is closed or not.
 boolean isConnected()
          Returns the connection state of the socket.
 DatagramPacket newPacket(byte[] data, int offset, int length)
           
 DatagramPacket newPacket(byte[] data, int offset, int length, java.net.SocketAddress addr)
           
 void receive(DatagramPacket p)
          Receives a datagram packet from this socket.
 void send(DatagramPacket p)
          Sends a datagram packet from this socket.
 void setReceiveBufferSize(int size)
          Sets the SO_RCVBUF option to the specified value for this DatagramSocket.
 void setReuseAddress(boolean on)
          Enable/disable the SO_REUSEADDR socket option.
 void setSendBufferSize(int size)
          Sets the SO_SNDBUF option to the specified value for this DatagramSocket.
 void setSoTimeout(int timeout)
          Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InetDatagramSocket

public InetDatagramSocket(java.net.DatagramSocket socket)
                   throws java.net.SocketException
Method Detail

newPacket

public DatagramPacket newPacket(byte[] data,
                                int offset,
                                int length)
Specified by:
newPacket in interface DatagramSocket
Returns:
a new instance of a datagram packet

newPacket

public DatagramPacket newPacket(byte[] data,
                                int offset,
                                int length,
                                java.net.SocketAddress addr)
                         throws java.net.SocketException
Specified by:
newPacket in interface DatagramSocket
Returns:
a new instance of a datagram packet
Throws:
java.net.SocketException

bind

public void bind(java.net.SocketAddress addr)
          throws java.net.SocketException
Binds this DatagramSocket to a specific address & port.

If the address is null, then the system will pick up an ephemeral port and a valid local address to bind the socket.

Specified by:
bind in interface DatagramSocket
Parameters:
addr - The address & port to bind to.
Throws:
java.lang.IllegalArgumentException - if addr is a SocketAddress subclass not supported by this socket.
java.net.SocketException - if any error happens during the bind, or if the socket is already bound.
java.lang.SecurityException - if a security manager exists and its checkListen method doesn't allow the operation.

isBound

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

Specified by:
isBound in interface DatagramSocket
Returns:
true if the socket succesfuly bound to an address
Since:
1.4

connect

public void connect(java.net.SocketAddress addr)
             throws java.net.SocketException
Connects this socket to a remote socket address (IP address + port number).

Specified by:
connect in interface DatagramSocket
Parameters:
addr - The remote address.
Throws:
java.net.SocketException - if the connect fails
java.lang.IllegalArgumentException - if addr is null or addr is a SocketAddress subclass not supported by this socket
See Also:
connect(java.net.SocketAddress)

disconnect

public void disconnect()
Disconnects the socket. This does nothing if the socket is not connected.

Specified by:
disconnect in interface DatagramSocket
See Also:
connect(java.net.SocketAddress)

isConnected

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

Specified by:
isConnected in interface DatagramSocket
Returns:
true if the socket succesfuly connected to a server
Since:
1.4

getLocalPort

public int getLocalPort()
Returns the port number on the local host to which this socket is bound.

Specified by:
getLocalPort in interface DatagramSocket
Returns:
the port number on the local host to which this socket is bound.

getLocalSocketAddress

public java.net.SocketAddress getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.

Specified by:
getLocalSocketAddress in interface DatagramSocket
Returns:
a SocketAddress representing the local endpoint of this socket, or null if it is not bound yet.
Since:
1.4
See Also:
getLocalPort(), bind(java.net.SocketAddress)

getPort

public int getPort()
Returns the port for this socket. Returns -1 if the socket is not connected.

Specified by:
getPort in interface DatagramSocket
Returns:
the port to which this socket is connected.

getRemoteSocketAddress

public java.net.SocketAddress getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

Specified by:
getRemoteSocketAddress in interface DatagramSocket
Returns:
a SocketAddress representing the remote endpoint of this socket, or null if it is not connected yet.
Since:
1.4
See Also:
getPort(), connect(java.net.SocketAddress)

receive

public void receive(DatagramPacket p)
             throws java.io.IOException
Receives a datagram packet from this socket. When this method returns, the DatagramPacket's buffer is filled with the data received. The datagram packet also contains the sender's IP address, and the port number on the sender's machine.

This method blocks until a datagram is received. The length field of the datagram packet object contains the length of the received message. If the message is longer than the packet's length, the message is truncated.

If there is a security manager, a packet cannot be received if the security manager's checkAccept method does not allow it.

Specified by:
receive in interface DatagramSocket
Parameters:
p - the DatagramPacket into which to place the incoming data.
Throws:
java.io.IOException - if an I/O error occurs.
java.net.SocketTimeoutException - if setSoTimeout was previously called and the timeout has expired.
java.net.PortUnreachableException - may be thrown if the socket is connected to a currently unreachable destination. Note, there is no guarantee that the exception will be thrown.
java.nio.channels.IllegalBlockingModeException - if this socket has an associated channel, and the channel is in non-blocking mode.
See Also:
DatagramPacket, DatagramSocket

send

public void send(DatagramPacket p)
          throws java.io.IOException
Sends a datagram packet from this socket. The DatagramPacket includes information indicating the data to be sent, its length, the IP address of the remote host, and the port number on the remote host.

If there is a security manager, and the socket is not currently connected to a remote address, this method first performs some security checks. First, if p.getAddress().isMulticastAddress() is true, this method calls the security manager's checkMulticast method with p.getAddress() as its argument. If the evaluation of that expression is false, this method instead calls the security manager's checkConnect method with arguments p.getAddress().getHostAddress() and p.getPort(). Each call to a security manager method could result in a SecurityException if the operation is not allowed.

Specified by:
send in interface DatagramSocket
Parameters:
p - the DatagramPacket to be sent.
Throws:
java.io.IOException - if an I/O error occurs.
java.lang.SecurityException - if a security manager exists and its checkMulticast or checkConnect method doesn't allow the send.
java.net.PortUnreachableException - may be thrown if the socket is connected to a currently unreachable destination. Note, there is no guarantee that the exception will be thrown.
java.nio.channels.IllegalBlockingModeException - if this socket has an associated channel, and the channel is in non-blocking mode.
See Also:
DatagramPacket, SecurityManager.checkMulticast(java.net.InetAddress), SecurityManager.checkConnect(java.lang.String, int)

close

public void close()
Closes this datagram socket.

Any thread currently blocked in {#link receive} upon this socket will throw a SocketException.

If this socket has an associated channel then the channel is closed as well.

Specified by:
close in interface DatagramSocket

isClosed

public boolean isClosed()
Returns whether the socket is closed or not.

Specified by:
isClosed in interface DatagramSocket
Returns:
true if the socket has been closed
Since:
1.4

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws java.net.SocketException
Get value of the SO_RCVBUF option for this DatagramSocket, that is the buffer size used by the platform for input on this DatagramSocket.

Specified by:
getReceiveBufferSize in interface DatagramSocket
Returns:
the value of the SO_RCVBUF option for this DatagramSocket
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as an UDP error.
See Also:
setReceiveBufferSize(int)

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws java.net.SocketException
Sets the SO_RCVBUF option to the specified value for this DatagramSocket. The SO_RCVBUF option is used by the the network implementation as a hint to size the underlying network I/O buffers. The SO_RCVBUF setting may also be used by the network implementation to determine the maximum size of the packet that can be received on this socket.

Because SO_RCVBUF is a hint, applications that want to verify what size the buffers were set to should call getReceiveBufferSize().

Increasing SO_RCVBUF may allow the network implementation to buffer multiple packets when packets arrive faster than are being received using receive(DatagramPacket).

Note: It is implementation specific if a packet larger than SO_RCVBUF can be received.

Specified by:
setReceiveBufferSize in interface DatagramSocket
Parameters:
size - the size to which to set the receive buffer size. This value must be greater than 0.
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as an UDP error.
java.lang.IllegalArgumentException - if the value is 0 or is negative.
See Also:
getReceiveBufferSize()

getReuseAddress

public boolean getReuseAddress()
                        throws java.net.SocketException
Tests if SO_REUSEADDR is enabled.

Specified by:
getReuseAddress in interface DatagramSocket
Returns:
a boolean indicating whether or not SO_REUSEADDR is enabled.
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as an UDP error.
Since:
1.4
See Also:
setReuseAddress(boolean)

setReuseAddress

public void setReuseAddress(boolean on)
                     throws java.net.SocketException
Enable/disable the SO_REUSEADDR socket option.

For UDP sockets it may be necessary to bind more than one socket to the same socket address. This is typically for the purpose of receiving multicast packets (See MulticastSocket). The SO_REUSEADDR socket option allows multiple sockets to be bound to the same socket address if the SO_REUSEADDR socket option is enabled prior to binding the socket using bind(java.net.SocketAddress).

When a DatagramSocket is created the initial setting of SO_REUSEADDR is disabled.

The behaviour when SO_REUSEADDR is enabled or disabled after a socket is bound (See isBound()) is not defined.

Specified by:
setReuseAddress in interface DatagramSocket
Parameters:
on - whether to enable or disable the
Throws:
java.net.SocketException - if an error occurs enabling or disabling the SO_RESUEADDR socket option, or the socket is closed.
Since:
1.4
See Also:
getReuseAddress(), bind(java.net.SocketAddress), isBound(), isClosed()

getSendBufferSize

public int getSendBufferSize()
                      throws java.net.SocketException
Get value of the SO_SNDBUF option for this DatagramSocket, that is the buffer size used by the platform for output on this DatagramSocket.

Specified by:
getSendBufferSize in interface DatagramSocket
Returns:
the value of the SO_SNDBUF option for this DatagramSocket
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as an UDP error.
See Also:
setSendBufferSize(int)

setSendBufferSize

public void setSendBufferSize(int size)
                       throws java.net.SocketException
Sets the SO_SNDBUF option to the specified value for this DatagramSocket. The SO_SNDBUF option is used by the network implementation as a hint to size the underlying network I/O buffers. The SO_SNDBUF setting may also be used by the network implementation to determine the maximum size of the packet that can be sent on this socket.

As SO_SNDBUF is a hint, applications that want to verify what size the buffer is should call getSendBufferSize().

Increasing the buffer size may allow multiple outgoing packets to be queued by the network implementation when the send rate is high.

Note: If send(DatagramPacket) is used to send a DatagramPacket that is larger than the setting of SO_SNDBUF then it is implementation specific if the packet is sent or discarded.

Specified by:
setSendBufferSize in interface DatagramSocket
Parameters:
size - the size to which to set the send buffer size. This value must be greater than 0.
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as an UDP error.
java.lang.IllegalArgumentException - if the value is 0 or is negative.
See Also:
getSendBufferSize()

getSoTimeout

public int getSoTimeout()
                 throws java.net.SocketException
Retrive setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).

Specified by:
getSoTimeout in interface DatagramSocket
Returns:
the setting for SO_TIMEOUT
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as an UDP error.
Since:
JDK1.1
See Also:
setSoTimeout(int)

setSoTimeout

public void setSoTimeout(int timeout)
                  throws java.net.SocketException
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to receive() for this DatagramSocket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the DatagramSocket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

Specified by:
setSoTimeout in interface DatagramSocket
Parameters:
timeout - the specified timeout in milliseconds.
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as an UDP error.
Since:
JDK1.1
See Also:
getSoTimeout()