DatagramSocket

public class DatagramSocket extends Object
implements Closeable
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.

Public Constructor Summary

DatagramSocket()
Constructs a UDP datagram socket which is bound to any available port on the localhost.
DatagramSocket(int aPort)
Constructs a UDP datagram socket which is bound to the specific port aPort on the localhost.
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 Constructor Summary

DatagramSocket(DatagramSocketImpl socketImpl)
Constructs a new DatagramSocket using the specific datagram socket implementation socketImpl.

Public Method Summary

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(InetAddress address, int port)
Connects this datagram socket to the specific address and port.
void
connect(SocketAddress peer)
Connects this datagram socket to the address and port specified by peer.
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.
synchronized 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.
synchronized int
getSendBufferSize()
Returns this socket's send buffer size.
synchronized 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.
synchronized 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.
synchronized static void
setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
Sets the socket implementation factory.
synchronized void
setReceiveBufferSize(int size)
Sets this socket's receive buffer size.
void
setReuseAddress(boolean reuse)
Sets the socket option SocketOptions.SO_REUSEADDR.
synchronized void
setSendBufferSize(int size)
Sets this socket's send buffer size.
synchronized 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 Method Summary