Android APIs
public class

DatagramSocket

extends Object
implements Closeable
java.lang.Object
   ↳ java.net.DatagramSocket
Known Direct Subclasses

Class Overview

This class implements a UDP socket for sending and receiving DatagramPacket. A DatagramSocket object can be used for both endpoints of a connection for a packet delivery service.

Summary

Public Constructors
DatagramSocket()
Constructs a UDP datagram socket which is bound to any available port on the local host using a wildcard address.
DatagramSocket(int aPort)
Constructs a UDP datagram socket which is bound to the specific port aPort on the local host using a wildcard address.
DatagramSocket(int aPort, InetAddress addr)
Constructs a UDP datagram socket which is bound to the specific local address addr on port aPort.
DatagramSocket(SocketAddress localAddr)
Constructs a new DatagramSocket bound to the host/port specified by the SocketAddress localAddr or an unbound DatagramSocket if the SocketAddress is null.
Protected Constructors
DatagramSocket(DatagramSocketImpl socketImpl)
Constructs a new DatagramSocket using the specific datagram socket implementation socketImpl.
Public Methods
void bind(SocketAddress localAddr)
Binds this socket to the local address and port specified by localAddr.
void close()
Closes this UDP datagram socket and all possibly associated channels.
void connect(SocketAddress peer)
Connects this datagram socket to the address and port specified by peer.
void connect(InetAddress address, int port)
Connects this datagram socket to the specific address and port.
void disconnect()
Disconnects this UDP datagram socket from the remote host.
boolean getBroadcast()
Gets the state of the socket option SocketOptions.SO_BROADCAST.
DatagramChannel getChannel()
Returns this socket's DatagramChannel, if one exists.
InetAddress getInetAddress()
Gets the InetAddress instance representing the remote address to which this UDP datagram socket is connected.
InetAddress getLocalAddress()
Returns the local address to which this socket is bound, a wildcard address if this socket is not yet bound, or null if this socket is closed.
int getLocalPort()
Gets the local port which this socket is bound to.
SocketAddress getLocalSocketAddress()
Returns the SocketAddress this socket is bound to, or null for an unbound or closed socket.
int getPort()
Gets the remote port which this socket is connected to.
int getReceiveBufferSize()
Returns this socket's receive buffer size.
SocketAddress getRemoteSocketAddress()
Returns the SocketAddress this socket is connected to, or null for an unconnected socket.
boolean getReuseAddress()
Gets the state of the socket option SocketOptions.SO_REUSEADDR.
int getSendBufferSize()
Returns this socket's send buffer size.
int getSoTimeout()
Gets the socket receive timeout.
int getTrafficClass()
Returns this socket's setting.
boolean isBound()
Returns true if this socket is bound to a local address.
boolean isClosed()
Gets the state of this socket.
boolean isConnected()
Returns true if this datagram socket is connected to a remote address.
void receive(DatagramPacket pack)
Receives a packet from this socket and stores it in the argument pack.
void send(DatagramPacket pack)
Sends a packet over this socket.
void setBroadcast(boolean broadcast)
Sets the socket option SocketOptions.SO_BROADCAST.
static void setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
Sets the socket implementation factory.
void setReceiveBufferSize(int size)
Sets this socket's receive buffer size.
void setReuseAddress(boolean reuse)
Sets the socket option SocketOptions.SO_REUSEADDR.
void setSendBufferSize(int size)
Sets this socket's send buffer size.
void setSoTimeout(int timeout)
Sets the read timeout in milliseconds for this socket.
void setTrafficClass(int value)
Sets the value for every packet sent by this socket.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public Constructors

public DatagramSocket ()

Added in API level 1

Constructs a UDP datagram socket which is bound to any available port on the local host using a wildcard address.

Throws
SocketException if an error occurs while creating or binding the socket.

public DatagramSocket (int aPort)

Added in API level 1

Constructs a UDP datagram socket which is bound to the specific port aPort on the local host using a wildcard address. Valid values for aPort are between 0 and 65535 inclusive.

Parameters
aPort int: the port to bind on the local host.
Throws
SocketException if an error occurs while creating or binding the socket.

public DatagramSocket (int aPort, InetAddress addr)

Added in API level 1

Constructs a UDP datagram socket which is bound to the specific local address addr on port aPort. Valid values for aPort are between 0 and 65535 inclusive. If addr is null the socket will be bound to a wildcard address.

Parameters
aPort int: the port to bind on the local host.
addr InetAddress: the address to bind on the local host.
Throws
SocketException if an error occurs while creating or binding the socket.

public DatagramSocket (SocketAddress localAddr)

Added in API level 1

Constructs a new DatagramSocket bound to the host/port specified by the SocketAddress localAddr or an unbound DatagramSocket if the SocketAddress is null.

Parameters
localAddr SocketAddress: the local machine address and port to bind to.
Throws
IllegalArgumentException if the SocketAddress is not supported
SocketException if a problem occurs creating or binding the socket.

Protected Constructors

protected DatagramSocket (DatagramSocketImpl socketImpl)

Added in API level 1

Constructs a new DatagramSocket using the specific datagram socket implementation socketImpl. The created DatagramSocket will not be bound.

Parameters
socketImpl DatagramSocketImpl: the DatagramSocketImpl to use.

Public Methods

public void bind (SocketAddress localAddr)

Added in API level 1

Binds this socket to the local address and port specified by localAddr. If this value is null any free port on a valid local address is used.

Parameters
localAddr SocketAddress: the local machine address and port to bind on.
Throws
IllegalArgumentException if the SocketAddress is not supported
SocketException if the socket is already bound or a problem occurs during binding.

public void close ()

Added in API level 1

Closes this UDP datagram socket and all possibly associated channels.

public void connect (SocketAddress peer)

Added in API level 1

Connects this datagram socket to the address and port specified by peer. Future calls to send(DatagramPacket) will use this as the default target, and receive(DatagramPacket) will only accept packets from this source.

Parameters
peer SocketAddress
Throws
SocketException if an error occurs.

public void connect (InetAddress address, int port)

Added in API level 1

Connects this datagram socket to the specific address and port. Future calls to send(DatagramPacket) will use this as the default target, and receive(DatagramPacket) will only accept packets from this source.

Beware: because it can't throw, this method silently ignores failures. Use connect(SocketAddress) instead.

Parameters
address InetAddress
port int

public void disconnect ()

Added in API level 1

Disconnects this UDP datagram socket from the remote host. This method called on an unconnected socket does nothing.

public boolean getBroadcast ()

Added in API level 1

Gets the state of the socket option SocketOptions.SO_BROADCAST.

Returns
boolean true if the option is enabled, false otherwise.
Throws
SocketException if the socket is closed or the option is invalid.

public DatagramChannel getChannel ()

Added in API level 1

Returns this socket's DatagramChannel, if one exists. A channel is available only if this socket wraps a channel. (That is, you can go from a channel to a socket and back again, but you can't go from an arbitrary socket to a channel.) In practice, this means that the socket must have been created by open().

Returns
DatagramChannel

public InetAddress getInetAddress ()

Added in API level 1

Gets the InetAddress instance representing the remote address to which this UDP datagram socket is connected.

Returns
InetAddress the remote address this socket is connected to or null if this socket is not connected.

public InetAddress getLocalAddress ()

Added in API level 1

Returns the local address to which this socket is bound, a wildcard address if this socket is not yet bound, or null if this socket is closed.

Returns
InetAddress

public int getLocalPort ()

Added in API level 1

Gets the local port which this socket is bound to.

Returns
int the local port of this socket or -1 if this socket is closed and 0 if it is unbound.

public SocketAddress getLocalSocketAddress ()

Added in API level 1

Returns the SocketAddress this socket is bound to, or null for an unbound or closed socket.

Returns
SocketAddress

public int getPort ()

Added in API level 1

Gets the remote port which this socket is connected to.

Returns
int the remote port of this socket. The return value -1 indicates that this socket is not connected.

public int getReceiveBufferSize ()

Added in API level 1

Returns this socket's receive buffer size.

Returns
int
Throws
SocketException

public SocketAddress getRemoteSocketAddress ()

Added in API level 1

Returns the SocketAddress this socket is connected to, or null for an unconnected socket.

Returns
SocketAddress

public boolean getReuseAddress ()

Added in API level 1

Gets the state of the socket option SocketOptions.SO_REUSEADDR.

Returns
boolean true if the option is enabled, false otherwise.
Throws
SocketException if the socket is closed or the option is invalid.

public int getSendBufferSize ()

Added in API level 1

Returns this socket's send buffer size.

Returns
int
Throws
SocketException

public int getSoTimeout ()

Added in API level 1

Gets the socket receive timeout.

Returns
int
Throws
SocketException if an error occurs while getting the option value.

public int getTrafficClass ()

Added in API level 1

Returns this socket's setting.

Returns
int
Throws
SocketException if the socket is closed or the option is invalid.

See also:

public boolean isBound ()

Added in API level 1

Returns true if this socket is bound to a local address. See bind(SocketAddress).

Returns
boolean

public boolean isClosed ()

Added in API level 1

Gets the state of this socket.

Returns
boolean true if the socket is closed, false otherwise.

public boolean isConnected ()

Added in API level 1

Returns true if this datagram socket is connected to a remote address. See connect(InetAddress, int).

Returns
boolean

public void receive (DatagramPacket pack)

Added in API level 1

Receives a packet from this socket and stores it in the argument pack. All fields of pack must be set according to the data received. If the received data is longer than the packet buffer size it is truncated. This method blocks until a packet is received or a timeout has expired.

Parameters
pack DatagramPacket: the DatagramPacket to store the received data.
Throws
IOException if an error occurs while receiving the packet.

public void send (DatagramPacket pack)

Added in API level 1

Sends a packet over this socket.

Parameters
pack DatagramPacket: the DatagramPacket which has to be sent.
Throws
IOException if an error occurs while sending the packet.

public void setBroadcast (boolean broadcast)

Added in API level 1

Sets the socket option SocketOptions.SO_BROADCAST. This option must be enabled to send broadcast messages.

Parameters
broadcast boolean: the socket option value to enable or disable this option.
Throws
SocketException if the socket is closed or the option could not be set.

public static void setDatagramSocketImplFactory (DatagramSocketImplFactory fac)

Added in API level 1

Sets the socket implementation factory. This may only be invoked once over the lifetime of the application. This factory is used to create a new datagram socket implementation.

Parameters
fac DatagramSocketImplFactory: the socket factory to use.
Throws
IOException if the factory has already been set.

public void setReceiveBufferSize (int size)

Added in API level 1

Sets this socket's receive buffer size.

Parameters
size int
Throws
SocketException

public void setReuseAddress (boolean reuse)

Added in API level 1

Sets the socket option SocketOptions.SO_REUSEADDR. This option has to be enabled if more than one UDP socket wants to be bound to the same address. That could be needed for receiving multicast packets.

There is an undefined behavior if this option is set after the socket is already bound.

Parameters
reuse boolean: the socket option value to enable or disable this option.
Throws
SocketException if the socket is closed or the option could not be set.

public void setSendBufferSize (int size)

Added in API level 1

Sets this socket's send buffer size.

Parameters
size int
Throws
SocketException

public void setSoTimeout (int timeout)

Added in API level 1

Sets the read timeout in milliseconds for this socket. This receive timeout defines the period the socket will block waiting to receive data before throwing an InterruptedIOException. The value 0 (default) is used to set an infinite timeout. To have effect this option must be set before the blocking method was called.

Parameters
timeout int: the timeout in milliseconds or 0 for no timeout.
Throws
SocketException if an error occurs while setting the option.

public void setTrafficClass (int value)

Added in API level 1

Sets the value for every packet sent by this socket.

Parameters
value int
Throws
SocketException if the socket is closed or the option could not be set.

See also: