public class DatagramSocket extends Object
Constructor and Description |
---|
DatagramSocket()
Initializes a new instance of
DatagramSocket that binds to
a random port and every address on the local machine. |
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 bindAddress)
Initializes a new instance of
DatagramSocket that binds to
the specified local port and address. |
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
connect(InetAddress address,
int port)
This method connects this socket to the specified address and port.
|
void |
disconnect()
This method disconnects this socket from the address/port it was
connected to.
|
boolean |
getBroadcast()
Checks if SO_BROADCAST is enabled
|
InetAddress |
getInetAddress() |
InetAddress |
getLocalAddress() |
int |
getLocalPort() |
int |
getPort() |
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.
|
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() |
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 on)
Enables/Disables SO_BROADCAST
|
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) |
void |
setTrafficClass(int tc)
Sets the traffic class value
|
public 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 bindAddress) throws SocketException
DatagramSocket
that binds to
the specified local port and address.port
- The local port number to bind to.bindAddress
- 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 void connect(InetAddress address, int port)
address
- The address to connect this socket to.port
- The port to connect this socket to.SocketException
- If an error occurs.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 InetAddress getInetAddress()
public int getPort()
public int getLocalPort()
public InetAddress getLocalAddress()
public void close()
public int getSoTimeout() throws SocketException
SocketException
public void setSoTimeout(int timeout) throws SocketException
SocketException
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 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.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 boolean isClosed()
public boolean isBound()
public boolean isConnected()
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 on) throws SocketException
on
- Whether or not to have SO_BROADCAST turned onSocketException
- 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)