java.net
public class DatagramSocket extends Object
Modifier | Constructor and Description |
---|---|
|
DatagramSocket()
Initializes a new instance of
DatagramSocket that binds to
a random port and every address on the local machine. |
protected |
DatagramSocket(DatagramSocketImpl impl)
Creates a
DatagramSocket from a specified
DatagramSocketImpl instance |
|
DatagramSocket(int port)
Initializes a new instance of
DatagramSocket that binds to
the specified port and every address on the local machine. |
|
DatagramSocket(int port,
InetAddress addr)
Initializes a new instance of
DatagramSocket that binds to
the specified local port and address. |
|
DatagramSocket(SocketAddress address)
Initializes a new instance of
DatagramSocket that binds to
the specified local port and address. |
Modifier and Type | Method and Description |
---|---|
void |
bind(SocketAddress address)
Binds the socket to the given socket address.
|
void |
close()
Closes this datagram socket.
|
void |
connect(InetAddress address,
int port)
This method connects this socket to the specified address and port.
|
void |
connect(SocketAddress address)
Connects the datagram socket to a specified socket address.
|
void |
disconnect()
This method disconnects this socket from the address/port it was
connected to.
|
boolean |
getBroadcast()
Checks if SO_BROADCAST is enabled
|
DatagramChannel |
getChannel()
Returns the datagram channel assoziated with this datagram socket.
|
InetAddress |
getInetAddress()
This method returns the remote address to which this socket is
connected.
|
InetAddress |
getLocalAddress()
Returns the local address this datagram socket is bound to.
|
int |
getLocalPort()
Returns the local port this socket is bound to.
|
SocketAddress |
getLocalSocketAddress()
Returns the local SocketAddress this socket is bound to.
|
int |
getPort()
This method returns the remote port to which this socket is
connected.
|
int |
getReceiveBufferSize()
This method returns the value of the system level socket option
SO_RCVBUF, which is used by the operating system to tune buffer
sizes for data transfers.
|
SocketAddress |
getRemoteSocketAddress()
Returns the SocketAddress of the host this socket is conneted to
or null if this socket is not connected.
|
boolean |
getReuseAddress()
Checks if SO_REUSEADDR is enabled.
|
int |
getSendBufferSize()
This method returns the value of the system level socket option
SO_SNDBUF, which is used by the operating system to tune buffer
sizes for data transfers.
|
int |
getSoTimeout()
Returns the value of the socket's SO_TIMEOUT setting.
|
int |
getTrafficClass()
Returns the current traffic class
|
boolean |
isBound()
Returns the binding state of the socket.
|
boolean |
isClosed()
Checks if the datagram socket is closed.
|
boolean |
isConnected()
Returns the connection state of the socket.
|
void |
receive(DatagramPacket p)
Reads a datagram packet from the socket.
|
void |
send(DatagramPacket p)
Sends the specified packet.
|
void |
setBroadcast(boolean enable)
Enables/Disables SO_BROADCAST
|
static void |
setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
Sets the datagram socket implementation factory for the application
|
void |
setReceiveBufferSize(int size)
This method sets the value for the system level socket option
SO_RCVBUF to the specified value.
|
void |
setReuseAddress(boolean on)
Enables/Disables SO_REUSEADDR.
|
void |
setSendBufferSize(int size)
This method sets the value for the system level socket option
SO_SNDBUF to the specified value.
|
void |
setSoTimeout(int timeout)
Sets the value of the socket's SO_TIMEOUT value.
|
void |
setTrafficClass(int tc)
Sets the traffic class value
|
protected DatagramSocket(DatagramSocketImpl impl)
DatagramSocket
from a specified
DatagramSocketImpl
instanceimpl
- The DatagramSocketImpl
the socket will be
created frompublic DatagramSocket() throws SocketException
DatagramSocket
that binds to
a random port and every address on the local machine.SocketException
- If an error occurs.SecurityException
- If a security manager exists and
its checkListen
method doesn't allow the operation.public DatagramSocket(int port) throws SocketException
DatagramSocket
that binds to
the specified port and every address on the local machine.port
- The local port number to bind to.SecurityException
- If a security manager exists and its
checkListen
method doesn't allow the operation.SocketException
- If an error occurs.public DatagramSocket(int port, InetAddress addr) throws SocketException
DatagramSocket
that binds to
the specified local port and address.port
- The local port number to bind to.addr
- The local address to bind to.SecurityException
- If a security manager exists and its
checkListen method doesn't allow the operation.SocketException
- If an error occurs.public DatagramSocket(SocketAddress address) throws SocketException
DatagramSocket
that binds to
the specified local port and address.address
- The local address and port number to bind to.SecurityException
- If a security manager exists and its
checkListen
method doesn't allow the operation.SocketException
- If an error occurs.public void close()
public InetAddress getInetAddress()
null
.public int getPort()
public InetAddress getLocalAddress()
public int getLocalPort()
public int getSoTimeout() throws SocketException
SocketException
- If an error occurs.public void setSoTimeout(int timeout) throws SocketException
timeout
- The new SO_TIMEOUT value in milliseconds.SocketException
- If an error occurs.public int getSendBufferSize() throws SocketException
SocketException
- If an error occurs.public void setSendBufferSize(int size) throws SocketException
size
- The new send buffer size.SocketException
- If an error occurs.IllegalArgumentException
- If size is 0 or negative.public int getReceiveBufferSize() throws SocketException
SocketException
- If an error occurs.public void setReceiveBufferSize(int size) throws SocketException
size
- The new receive buffer size.SocketException
- If an error occurs.IllegalArgumentException
- If size is 0 or negative.public void connect(InetAddress address, int port)
address
- The address to connect this socket to.port
- The port to connect this socket to.IllegalArgumentException
- If address or port are invalid.SecurityException
- If the caller is not allowed to send
datagrams to or receive from this address and port.public void disconnect()
public void receive(DatagramPacket p) throws IOException
DatagramPacket
is populated with the data
received and all the other information about the packet.p
- A DatagramPacket
for storing the dataIOException
- If an error occurs.SocketTimeoutException
- If setSoTimeout was previously called
and the timeout has expired.PortUnreachableException
- If the socket is connected to a
currently unreachable destination. Note, there is no guarantee that the
exception will be thrown.IllegalBlockingModeException
- If this socket has an associated
channel, and the channel is in non-blocking mode.SecurityException
- If a security manager exists and its
checkAccept method doesn't allow the receive.public void send(DatagramPacket p) throws IOException
p
- The datagram packet to send.IOException
- If an error occurs.SecurityException
- If a security manager exists and its
checkMulticast or checkConnect method doesn't allow the send.PortUnreachableException
- If the socket is connected to a
currently unreachable destination. Note, there is no guarantee that the
exception will be thrown.IllegalBlockingModeException
- If this socket has an associated
channel, and the channel is in non-blocking mode.public void bind(SocketAddress address) throws SocketException
address
- The socket address to bind to.SocketException
- If an error occurs.SecurityException
- If a security manager exists and
its checkListen method doesn't allow the operation.IllegalArgumentException
- If address type is not supported.public boolean isClosed()
public DatagramChannel getChannel()
DatagramChannel
object or nullpublic void connect(SocketAddress address) throws SocketException
address
- The socket address to connect to.SocketException
- If an error occurs.IllegalArgumentException
- If address type is not supported.public boolean isBound()
public boolean isConnected()
public SocketAddress getRemoteSocketAddress()
public SocketAddress getLocalSocketAddress()
public void setReuseAddress(boolean on) throws SocketException
on
- Whether or not to have SO_REUSEADDR turned on.SocketException
- If an error occurs.public boolean getReuseAddress() throws SocketException
SocketException
- If an error occurs.public void setBroadcast(boolean enable) throws SocketException
enable
- True if SO_BROADCAST should be enabled, false otherwise.SocketException
- If an error occurspublic boolean getBroadcast() throws SocketException
SocketException
- If an error occurspublic void setTrafficClass(int tc) throws SocketException
tc
- The traffic classSocketException
- If an error occursIllegalArgumentException
- If tc value is illegalgetTrafficClass()
public int getTrafficClass() throws SocketException
SocketException
- If an error occurssetTrafficClass(int tc)
public static void setDatagramSocketImplFactory(DatagramSocketImplFactory fac) throws IOException
fac
- The factory to setIOException
- If an error occursSocketException
- If the factory is already definedSecurityException
- If a security manager exists and its
checkSetFactory method doesn't allow the operation