More information: http://www.cs.berkeley.edu/~mdw/proj/seda/

seda.nbio
Class NonblockingDatagramSocket

java.lang.Object
  |
  +--java.net.DatagramSocket
        |
        +--seda.nbio.NonblockingDatagramSocket
All Implemented Interfaces:
Selectable
Direct Known Subclasses:
NonblockingMulticastSocket

public class NonblockingDatagramSocket
extends java.net.DatagramSocket
implements Selectable

A NonblockingDatagramSocket provides non-blocking UDP (datagram) I/O.


Field Summary
(package private)  NonblockingSocketImpl impl
           
(package private)  boolean is_connected
           
 
Fields inherited from class java.net.DatagramSocket
connected, connectedAddress, connectedPort, factory, implClass
 
Fields inherited from interface seda.nbio.Selectable
ACCEPT_READY, CONNECT_READY, READ_READY, SELECT_ERROR, WRITE_READY
 
Constructor Summary
NonblockingDatagramSocket()
          Create a NonblockingDatagramSocket bound to any available port.
NonblockingDatagramSocket(int port)
          Create a NonblockingDatagramSocket bound to the given port.
NonblockingDatagramSocket(int port, java.net.InetAddress laddr)
          Create a NonblockingDatagramSocket bound to the given port and the given local address.
 
Method Summary
(package private) static void ()
           
 void close()
          Close this NonblockingDatagramSocket.
 void connect(java.net.InetAddress address, int port)
          Connect this NonblockingDatagramSocket to the given address and port.
 void connect(java.lang.String host, int port)
           
 void disconnect()
           
 java.net.InetAddress getInetAddress()
          Return the remote address to which this socket is bound.
 java.net.InetAddress getLocalAddress()
          Return the local address to which this socket is bound.
 int getLocalPort()
          Return the local port to which this socket is bound.
 int getPort()
          Return the remote port to which this socket is bound.
 int getReceiveBufferSize()
           
 int getSendBufferSize()
           
 int nbReceive(byte[] data, int offset, int length)
          Receive a datagram from this socket.
 int nbReceive(java.net.DatagramPacket p)
          Receive a datagram from this socket.
 int nbSend(byte[] data, int offset, int length)
          Sends a datagram packet from this socket.
 int nbSend(byte[] data, int offset, int length, java.net.InetAddress addr, int port)
          Sends a datagram packet from this socket.
 int nbSend(java.net.DatagramPacket p)
          Sends a datagram packet from this socket.
 void receive(java.net.DatagramPacket p)
          This method is provided for convenience and mimics blocking behavior by invoking the nonblocking nbReceive() operation.
 void send(java.net.DatagramPacket p)
          This method is provided for convenience and mimics blocking behavior by invoking the nonblocking nbSend() operation.
 void setReceiveBufferSize(int size)
           
 void setSendBufferSize(int size)
           
 
Methods inherited from class java.net.DatagramSocket
create, getSoTimeout, setDatagramSocketImplFactory, setSoTimeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

impl

NonblockingSocketImpl impl

is_connected

boolean is_connected
Constructor Detail

NonblockingDatagramSocket

public NonblockingDatagramSocket()
                          throws java.io.IOException
Create a NonblockingDatagramSocket bound to any available port.

NonblockingDatagramSocket

public NonblockingDatagramSocket(int port)
                          throws java.io.IOException
Create a NonblockingDatagramSocket bound to the given port.

NonblockingDatagramSocket

public NonblockingDatagramSocket(int port,
                                 java.net.InetAddress laddr)
                          throws java.io.IOException
Create a NonblockingDatagramSocket bound to the given port and the given local address.
Method Detail

static void ()

close

public void close()
Close this NonblockingDatagramSocket.
Overrides:
close in class java.net.DatagramSocket

connect

public void connect(java.net.InetAddress address,
                    int port)
             throws java.lang.IllegalArgumentException
Connect this NonblockingDatagramSocket to the given address and port. All send() operations with a NULL 'sendTo' address will now send to this address by default. You may call connect() multiple times on a NonblockingDatagramSocket to change the default send address.
Overrides:
connect in class java.net.DatagramSocket

connect

public void connect(java.lang.String host,
                    int port)
             throws java.net.UnknownHostException

disconnect

public void disconnect()
Overrides:
disconnect in class java.net.DatagramSocket

getInetAddress

public java.net.InetAddress getInetAddress()
Return the remote address to which this socket is bound. Should be null if the socket hasn't been connected to anything.
Overrides:
getInetAddress in class java.net.DatagramSocket

getLocalAddress

public java.net.InetAddress getLocalAddress()
Return the local address to which this socket is bound.
Overrides:
getLocalAddress in class java.net.DatagramSocket

getPort

public int getPort()
Return the remote port to which this socket is bound.
Overrides:
getPort in class java.net.DatagramSocket

getLocalPort

public int getLocalPort()
Return the local port to which this socket is bound.
Overrides:
getLocalPort in class java.net.DatagramSocket

nbReceive

public int nbReceive(java.net.DatagramPacket p)
              throws java.io.IOException
Receive a datagram 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 does not block if a datagram is not ready to be received. The length field of the datagram packet object contains the length of the received message, or is set to 0 if no packet was received. If the message is longer than the packet's length, the message is truncated.
Returns:
The size of the received packet, or 0 if no data was received.

nbReceive

public int nbReceive(byte[] data,
                     int offset,
                     int length)
              throws java.io.IOException
Receive a datagram from this socket. When this method returns, the given byte array is filled with the data received starting at the given offset with the given length. If the message is longer than the given length, the message is truncated. This method does not block if a datagram is not ready to be received.
Returns:
The size of the received packet, or 0 if no data was received.

nbSend

public int nbSend(java.net.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. This method does not block; it returns 0 if the packet could not be sent.
Returns:
The amount of data sent, or 0 if the packet could not be sent immediately.

nbSend

public int nbSend(byte[] data,
                  int offset,
                  int length,
                  java.net.InetAddress addr,
                  int port)
           throws java.io.IOException
Sends a datagram packet from this socket. This method constructs a temporary DatagramPacket from the given data, offset, length, address, and port, and calls nbSend(DatagramPacket p). This method does not block; it returns 0 if the packet could not be sent.
Returns:
The amount of data sent, or 0 if the packet could not be sent immediately.

nbSend

public int nbSend(byte[] data,
                  int offset,
                  int length)
           throws java.io.IOException
Sends a datagram packet from this socket. This method constructs a temporary DatagramPacket from the given data, offset, and length. This method may only be called on a connected socket; if called on an unconnected socket, an IllegalArgumentException is thrown. This method does not block; it returns 0 if the packet could not be sent.
Returns:
The amount of data sent, or 0 if the packet could not be sent immediately.

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws java.net.SocketException
Overrides:
getReceiveBufferSize in class java.net.DatagramSocket

getSendBufferSize

public int getSendBufferSize()
                      throws java.net.SocketException
Overrides:
getSendBufferSize in class java.net.DatagramSocket

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws java.net.SocketException
Overrides:
setReceiveBufferSize in class java.net.DatagramSocket

setSendBufferSize

public void setSendBufferSize(int size)
                       throws java.net.SocketException
Overrides:
setSendBufferSize in class java.net.DatagramSocket

send

public void send(java.net.DatagramPacket p)
          throws java.io.IOException
This method is provided for convenience and mimics blocking behavior by invoking the nonblocking nbSend() operation. If the packet could not be immediately sent it is simply dropped (because this is a UDP socket this behavior is consistent with the lack of reliability in UDP).

Use of this method is not recommended and is provided only for compatibility with java.net.DatagramSocket.

Overrides:
send in class java.net.DatagramSocket

receive

public void receive(java.net.DatagramPacket p)
             throws java.io.IOException
This method is provided for convenience and mimics blocking behavior by invoking the nonblocking nbReceive() operation. If no packet could be immediately received it returns immediately with a received packet length of 0.

Use of this method is not recommended and is provided only for compatibility with java.net.DatagramSocket.

Overrides:
receive in class java.net.DatagramSocket

More information: http://www.cs.berkeley.edu/~mdw/proj/seda/