public class DatagramSocket extends Object
DatagramPacket
. A DatagramSocket
object can be used for both
endpoints of a connection for a packet delivery service.DatagramPacket
,
DatagramSocketImplFactory
Modifier | Constructor and Description |
---|---|
|
DatagramSocket()
Constructs a UDP datagram socket which is bound to any available port on
the localhost.
|
protected |
DatagramSocket(DatagramSocketImpl socketImpl)
Constructs a new
DatagramSocket using the specific datagram
socket implementation socketImpl . |
|
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 . |
Modifier and Type | Method and Description |
---|---|
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. |
FileDescriptor |
getFileDescriptor$() |
InetAddress |
getInetAddress()
Gets the
InetAddress instance representing the remote address to
which this UDP datagram socket is connected. |
InetAddress |
getLocalAddress()
Gets the
InetAddress instance representing the bound local
address of this UDP datagram socket. |
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 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
SocketOptions.IP_TOS 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 |
setNetworkInterface(NetworkInterface netInterface)
Sets the network interface used by this socket.
|
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
SocketOptions.IP_TOS value for every packet sent by this socket. |
public DatagramSocket() throws SocketException
SocketException
- if an error occurs while creating or binding the socket.public DatagramSocket(int aPort) throws SocketException
aPort
on the localhost. Valid values for aPort
are
between 0 and 65535 inclusive.aPort
- the port to bind on the localhost.SocketException
- if an error occurs while creating or binding the socket.public DatagramSocket(int aPort, InetAddress addr) throws SocketException
addr
on port aPort
. Valid values for aPort
are between 0 and 65535 inclusive.aPort
- the port to bind on the localhost.addr
- the address to bind on the localhost.SocketException
- if an error occurs while creating or binding the socket.protected DatagramSocket(DatagramSocketImpl socketImpl)
DatagramSocket
using the specific datagram
socket implementation socketImpl
. The created DatagramSocket
will not be bound.socketImpl
- the DatagramSocketImpl to use.public DatagramSocket(SocketAddress localAddr) throws SocketException
DatagramSocket
bound to the host/port specified
by the SocketAddress
localAddr
or an unbound DatagramSocket
if the SocketAddress
is null
.localAddr
- the local machine address and port to bind to.IllegalArgumentException
- if the SocketAddress is not supportedSocketException
- if a problem occurs creating or binding the socket.public void close()
public void disconnect()
public InetAddress getInetAddress()
InetAddress
instance representing the remote address to
which this UDP datagram socket is connected.null
if
this socket is not connected.public InetAddress getLocalAddress()
InetAddress
instance representing the bound local
address of this UDP datagram socket.null
if this socket is closed.public int getLocalPort()
-1
if this socket is
closed and 0
if it is unbound.public int getPort()
-1
indicates that this socket is not connected.public int getReceiveBufferSize() throws SocketException
receive buffer size
.SocketException
public int getSendBufferSize() throws SocketException
send buffer size
.SocketException
public int getSoTimeout() throws SocketException
receive timeout
.SocketException
- if an error occurs while getting the option value.public void receive(DatagramPacket pack) throws IOException
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.pack
- the DatagramPacket
to store the received data.IOException
- if an error occurs while receiving the packet.public void send(DatagramPacket pack) throws IOException
pack
- the DatagramPacket
which has to be sent.IOException
- if an error occurs while sending the packet.public void setNetworkInterface(NetworkInterface netInterface) throws SocketException
SocketException
public void setSendBufferSize(int size) throws SocketException
send buffer size
.SocketException
public void setReceiveBufferSize(int size) throws SocketException
receive buffer size
.SocketException
public void setSoTimeout(int timeout) throws SocketException
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.timeout
- the timeout in milliseconds or 0 for no timeout.SocketException
- if an error occurs while setting the option.public static void setDatagramSocketImplFactory(DatagramSocketImplFactory fac) throws IOException
fac
- the socket factory to use.IOException
- if the factory has already been set.DatagramSocketImplFactory
public void bind(SocketAddress localAddr) throws SocketException
localAddr
. If this value is null
any free port on a valid local
address is used.localAddr
- the local machine address and port to bind on.IllegalArgumentException
- if the SocketAddress is not supportedSocketException
- if the socket is already bound or a problem occurs during
binding.public void connect(SocketAddress peer) throws SocketException
peer
.
Future calls to send(java.net.DatagramPacket)
will use this as the default target, and receive(java.net.DatagramPacket)
will only accept packets from this source.SocketException
- if an error occurs.public void connect(InetAddress address, int port)
address
and port
.
Future calls to send(java.net.DatagramPacket)
will use this as the default target, and receive(java.net.DatagramPacket)
will only accept packets from this source.
Beware: because it can't throw, this method silently ignores failures.
Use connect(SocketAddress)
instead.
public boolean isBound()
bind(java.net.SocketAddress)
.public boolean isConnected()
connect(java.net.SocketAddress)
.public SocketAddress getRemoteSocketAddress()
SocketAddress
this socket is connected to, or null for an unconnected
socket.public SocketAddress getLocalSocketAddress()
SocketAddress
this socket is bound to, or null for an unbound socket.public void setReuseAddress(boolean reuse) throws SocketException
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.
reuse
- the socket option value to enable or disable this option.SocketException
- if the socket is closed or the option could not be set.public boolean getReuseAddress() throws SocketException
SocketOptions.SO_REUSEADDR
.true
if the option is enabled, false
otherwise.SocketException
- if the socket is closed or the option is invalid.public void setBroadcast(boolean broadcast) throws SocketException
SocketOptions.SO_BROADCAST
. This option
must be enabled to send broadcast messages.broadcast
- the socket option value to enable or disable this option.SocketException
- if the socket is closed or the option could not be set.public boolean getBroadcast() throws SocketException
SocketOptions.SO_BROADCAST
.true
if the option is enabled, false
otherwise.SocketException
- if the socket is closed or the option is invalid.public void setTrafficClass(int value) throws SocketException
SocketOptions.IP_TOS
value for every packet sent by this socket.SocketException
- if the socket is closed or the option could not be set.public int getTrafficClass() throws SocketException
SocketOptions.IP_TOS
setting.SocketException
- if the socket is closed or the option is invalid.public boolean isClosed()
true
if the socket is closed, false
otherwise.public DatagramChannel getChannel()
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
DatagramChannel.open()
.public final FileDescriptor getFileDescriptor$()