Skip to content

Most visited

Recently visited

navigation
Added in API level 1

DatagramSocket

public class DatagramSocket
extends Object implements Closeable

java.lang.Object
   ↳ java.net.DatagramSocket
Known Direct Subclasses


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.

See also:

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.

Inherited methods

From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public constructors

DatagramSocket

Added in API level 1
DatagramSocket ()

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.

DatagramSocket

Added in API level 1
DatagramSocket (int aPort)

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.

DatagramSocket

Added in API level 1
DatagramSocket (int aPort, 
                InetAddress addr)

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.

DatagramSocket

Added in API level 1
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.

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

DatagramSocket

Added in API level 1
DatagramSocket (DatagramSocketImpl socketImpl)

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

bind

Added in API level 1
void bind (SocketAddress localAddr)

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.

close

Added in API level 1
void close ()

Closes this UDP datagram socket and all possibly associated channels.

connect

Added in API level 1
void connect (SocketAddress peer)

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.

connect

Added in API level 1
void connect (InetAddress address, 
                int port)

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

disconnect

Added in API level 1
void disconnect ()

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

getBroadcast

Added in API level 1
boolean getBroadcast ()

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.

getChannel

Added in API level 1
DatagramChannel getChannel ()

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

getInetAddress

Added in API level 1
InetAddress getInetAddress ()

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.

getLocalAddress

Added in API level 1
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.

Returns
InetAddress

getLocalPort

Added in API level 1
int getLocalPort ()

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.

getLocalSocketAddress

Added in API level 1
SocketAddress getLocalSocketAddress ()

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

Returns
SocketAddress

getPort

Added in API level 1
int getPort ()

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.

getReceiveBufferSize

Added in API level 1
int getReceiveBufferSize ()

Returns this socket's receive buffer size.

Returns
int
Throws
SocketException

getRemoteSocketAddress

Added in API level 1
SocketAddress getRemoteSocketAddress ()

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

Returns
SocketAddress

getReuseAddress

Added in API level 1
boolean getReuseAddress ()

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.

getSendBufferSize

Added in API level 1
int getSendBufferSize ()

Returns this socket's send buffer size.

Returns
int
Throws
SocketException

getSoTimeout

Added in API level 1
int getSoTimeout ()

Gets the socket receive timeout.

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

getTrafficClass

Added in API level 1
int getTrafficClass ()

Returns this socket's setting.

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

See also:

isBound

Added in API level 1
boolean isBound ()

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

Returns
boolean

isClosed

Added in API level 1
boolean isClosed ()

Gets the state of this socket.

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

isConnected

Added in API level 1
boolean isConnected ()

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

Returns
boolean

receive

Added in API level 1
void receive (DatagramPacket pack)

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.

send

Added in API level 1
void send (DatagramPacket pack)

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.

setBroadcast

Added in API level 1
void setBroadcast (boolean broadcast)

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.

setDatagramSocketImplFactory

Added in API level 1
void setDatagramSocketImplFactory (DatagramSocketImplFactory fac)

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.

See also:

setReceiveBufferSize

Added in API level 1
void setReceiveBufferSize (int size)

Sets this socket's receive buffer size.

Parameters
size int
Throws
SocketException

setReuseAddress

Added in API level 1
void setReuseAddress (boolean reuse)

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.

setSendBufferSize

Added in API level 1
void setSendBufferSize (int size)

Sets this socket's send buffer size.

Parameters
size int
Throws
SocketException

setSoTimeout

Added in API level 1
void setSoTimeout (int timeout)

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.

setTrafficClass

Added in API level 1
void setTrafficClass (int value)

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:

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.