ewe.net
Class DatagramSocket

java.lang.Object
  extended byewe.net.DatagramSocket
All Implemented Interfaces:
StreamCanPause

public class DatagramSocket
extends Object
implements StreamCanPause

A DatagramSocket is used for sending/receiving datagram (connectionless) IP packets (UDP).


Field Summary
protected  Object localHost
           
protected  int localPortNotUsed
           
 
Fields inherited from interface ewe.io.StreamCanPause
PAUSE_UNTIL_CAN_READ, PAUSE_UNTIL_CAN_WRITE, PAUSE_UNTIL_CLOSED, PAUSE_UNTIL_FLUSHED
 
Constructor Summary
DatagramSocket()
          Create a DatagramSocket bound to all addresses for the local host and to the next available port number.
DatagramSocket(int port)
          Create a DatagramSocket bound to all addresses for the local host and to the specified port number.
DatagramSocket(int port, InetAddress addr)
          Create a DatagramSocket bound to a specific local host address and to a specified port number.
DatagramSocket(int port, String hostName)
          This is generally only used to connect to the infra-red port, in which case you should set hostName to InetAddress.INFRA_RED.
 
Method Summary
 void close()
           
 InetAddress getLocalAddress()
          Get the local address the DatagramSocket is bound to.
 int getLocalPort()
          Return the port number on the local host to which this socket is bound.
 int getReceiveBufferSize()
           
 int getSendBufferSize()
           
 int pauseUntilReady(int type, int time)
          Do not call this directly, it is used internally.
 void receive(DatagramPacket packet)
          Receive an incoming DatagramPacket.
 boolean receivePacket(DatagramPacket packet)
          This is a non-blocking receive.
 void send(DatagramPacket packet)
          Send a DatagramPacket This method will block until the data is sent.
 boolean sendPacket(DatagramPacket packet)
          This is a non-blocking send.
 void setReceiveBufferSize(int size)
           
 void setSendBufferSize(int size)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

localHost

protected Object localHost

localPortNotUsed

protected int localPortNotUsed
Constructor Detail

DatagramSocket

public DatagramSocket()
               throws SocketException
Create a DatagramSocket bound to all addresses for the local host and to the next available port number.

Throws:
SocketException - if the DatagramSocket could not be created.

DatagramSocket

public DatagramSocket(int port)
               throws SocketException
Create a DatagramSocket bound to all addresses for the local host and to the specified port number.

Parameters:
port - The port number to bind to. A port number of 0 requests the next available port.
Throws:
SocketException - if the DatagramSocket could not be created.

DatagramSocket

public DatagramSocket(int port,
                      InetAddress addr)
               throws SocketException
Create a DatagramSocket bound to a specific local host address and to a specified port number.

Parameters:
port - The port number to bind to. A port number of 0 requests the next available port.
addr - An InetAddress representing one of the addresses assigned to the local host. If this is null then the socket will be bound to all addresses.
Throws:
SocketException - if the DatagramSocket could not be created.

DatagramSocket

public DatagramSocket(int port,
                      String hostName)
               throws SocketException
This is generally only used to connect to the infra-red port, in which case you should set hostName to InetAddress.INFRA_RED. Note that Datagram (UDP) sockets on the infra-red port is not supported on some operating systems (e.g. WindowsCE/PocketPC and so an exception will be thrown).

Parameters:
port - The port number to bind to. A port number of 0 requests the next available port.
hostName - The local host address (in dotted notation or InetAddress.INFRA_RED) to bind to.
Throws:
SocketException - if the DatagramSocket could not be created.
Method Detail

getLocalPort

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

Returns:
the port number on the local host to which this socket is bound.

close

public void close()

getLocalAddress

public InetAddress getLocalAddress()
Get the local address the DatagramSocket is bound to.

Returns:
the local address the DatagramSocket is bound to.

pauseUntilReady

public int pauseUntilReady(int type,
                           int time)
Do not call this directly, it is used internally.

Specified by:
pauseUntilReady in interface StreamCanPause

receivePacket

public boolean receivePacket(DatagramPacket packet)
                      throws IOException
This is a non-blocking receive.

Parameters:
packet - The destination to place the data in.
Returns:
true if the packet was received, false if no packet was available for receiving.
Throws:
IOException - if an error occured receiving data.

sendPacket

public boolean sendPacket(DatagramPacket packet)
                   throws IOException
This is a non-blocking send.

Parameters:
packet - The data to send.
Returns:
true if the packet was sent, false if the packet could not be sent yet.
Throws:
IOException - if an error occured sending data.

receive

public void receive(DatagramPacket packet)
             throws IOException
Receive an incoming DatagramPacket. This method will block until data is received or an exception is thrown.

Parameters:
packet - This is used to hold the incoming data.
Throws:
IOException - if an error occurs while receiving data.

send

public void send(DatagramPacket packet)
          throws IOException
Send a DatagramPacket This method will block until the data is sent.

Parameters:
packet - This is used to hold the outgoing data.
Throws:
IOException - if an error occurs while sending the data.

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws SocketException
Throws:
SocketException

getSendBufferSize

public int getSendBufferSize()
                      throws SocketException
Throws:
SocketException

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws SocketException
Throws:
SocketException

setSendBufferSize

public void setSendBufferSize(int size)
                       throws SocketException
Throws:
SocketException