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 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
|
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
|
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
|
public DatagramSocket() throws SocketException
DatagramSocket
that binds to a
random port and every address on the local machine.SocketException
- If an error occurs.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.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.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.SocketException
- If an error occurs.public void bind(SocketAddress address) throws SocketException
address
- The socket address to bind to.SocketException
- If an error occurs.IllegalArgumentException
- If address type is not supported.public void close()
public void connect(InetAddress address, int port) throws SocketException, IllegalArgumentException
address
- The address to connect this socket to.port
- The port to connect this socket to.SocketException
IllegalArgumentException
- If address or port are invalid.public 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 void disconnect()
public boolean getBroadcast() throws SocketException
SocketException
- If an error occurspublic InetAddress getInetAddress()
null
.public InetAddress getLocalAddress()
public int getLocalPort()
public SocketAddress getLocalSocketAddress()
public int getPort()
public int getReceiveBufferSize() throws SocketException
SocketException
- If an error occurs.public SocketAddress getRemoteSocketAddress()
public boolean getReuseAddress() throws SocketException
SocketException
- If an error occurs.public int getSendBufferSize() throws SocketException
SocketException
- If an error occurs.public int getSoTimeout() throws SocketException
SocketException
- If an error occurs.public int getTrafficClass() throws SocketException
SocketException
- If an error occurssetTrafficClass(int tc)
public boolean isBound()
public boolean isClosed()
public boolean isConnected()
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.public void send(DatagramPacket p) throws IOException
p
- The datagram packet to send.IOException
- 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 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 setReuseAddress(boolean on) throws SocketException
on
- Whether or not to have SO_REUSEADDR turned on.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 void setSoTimeout(int timeout) throws SocketException
timeout
- The new SO_TIMEOUT value in milliseconds.SocketException
- If an error occurs.public void setTrafficClass(int tc) throws SocketException
tc
- The traffic classSocketException
- If an error occursIllegalArgumentException
- If tc value is illegalgetTrafficClass()