# inet::UdpSocket

    sys::Obj
      inet::UdpSocket

`public class UdpSocket` `[javaPeer]`

UdpSocket is used to send and receive UDP datagrams. The Sedona socket APIs are all asynchronous to be used in a single threaded environment.

READ

  `public static const define int READ`

  Bitmask for select when socket is readable

WRITE

  `public static const define int WRITE`

  Bitmask for select when socket is writable

bind

  `public native bool bind(int port)`

  Bind this socket the specified well-known port on this host. Return true on success, false on failure.

close

  `public native void close()`

  Shutdown and close this socket.

idealPacketSize

  `public static native int idealPacketSize()`

  What is the ideal maximum number of bytes to send by this UDP implementation. This is typically driven by the lower levels of the IP stack - for instance when running 6LoWPAN over 802.15.4, this is the max UDP packet size which doesn't require fragmenting across multiple 802.15.4 frames.

isClosed

  `public bool isClosed()`

  Is this socket closed.

join

  `public native bool join()`

  Join this socket to a multicast group. (Address is defined at native level.) Return true on success, false on failure.

maxPacketSize

  `public static native int maxPacketSize()`

  What is the maximum number of bytes which can be sent by this UDP implementation.

open

  `public native bool open()`

  Initialize this socket which allocates a socket handle for this instance. This method must be called before using the socket. Return true on success, false on failure.

receive

  `public native bool receive(UdpDatagram datagram)`

  Receive a datagram into the specified structure. The datagram.buf must reference a valid byte buffer - bytes are read in starting at datagram.buf[0] with at most datagram.len bytes being received. If successful then return true, datagram.len reflects the actual number of bytes received, and datagram.addr/datagram.port reflect the source socket address. Note datagram.addr is only valid until the next call to receive(). On failure or if no packets are pending to read, then return false, len=0, port=-1, and addr=null.

  Note if the number of bytes available to be read is greater then len than this call works differently dependent on the platform. In Java it silently ignores the remainder of the bytes (wrong way), and in C++ it returns false since the message received is not the same as the message sent.

send

  `public native bool send(UdpDatagram datagram)`

  Send the specified datagram which encapsulates both the destination address and the data to send. Return true on success, false on failure. If the number of bytes sent does not match datagram.len then this call will fail and return false.