GeezerVison is showing docs.oracle.com — DatagramSocket (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.net
Class DatagramSocket
-
-
Constructor Summary
Constructors
| Modifier |
Constructor |
Description |
|
DatagramSocket() |
Constructs a datagram socket and binds it to any available port
on the local host machine.
|
protected |
DatagramSocket(DatagramSocketImpl impl) |
Creates an unbound datagram socket with the specified
DatagramSocketImpl.
|
|
DatagramSocket(int port) |
Constructs a datagram socket and binds it to the specified port
on the local host machine.
|
|
DatagramSocket(int port,
InetAddress laddr) |
Creates a datagram socket, bound to the specified local
address.
|
|
DatagramSocket(SocketAddress bindaddr) |
Creates a datagram socket, bound to the specified local
socket address.
|
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
void |
bind(SocketAddress addr) |
Binds this DatagramSocket to a specific address and port.
|
void |
close() |
Closes this datagram socket.
|
void |
connect(InetAddress address,
int port) |
Connects the socket to a remote address for this socket.
|
void |
connect(SocketAddress addr) |
Connects this socket to a remote socket address (IP address + port number).
|
void |
disconnect() |
Disconnects the socket.
|
boolean |
getBroadcast() |
Tests if SO_BROADCAST is enabled.
|
DatagramChannel |
getChannel() |
Returns the unique DatagramChannel object
associated with this datagram socket, if any.
|
InetAddress |
getInetAddress() |
Returns the address to which this socket is connected.
|
InetAddress |
getLocalAddress() |
Gets the local address to which the socket is bound.
|
int |
getLocalPort() |
Returns the port number on the local host to which this socket
is bound.
|
SocketAddress |
getLocalSocketAddress() |
Returns the address of the endpoint this socket is bound to.
|
int |
getPort() |
Returns the port number to which this socket is connected.
|
int |
getReceiveBufferSize() |
Get value of the SO_RCVBUF option for this DatagramSocket, that is the
buffer size used by the platform for input on this DatagramSocket.
|
SocketAddress |
getRemoteSocketAddress() |
Returns the address of the endpoint this socket is connected to, or
null if it is unconnected.
|
boolean |
getReuseAddress() |
Tests if SO_REUSEADDR is enabled.
|
int |
getSendBufferSize() |
Get value of the SO_SNDBUF option for this DatagramSocket, that is the
buffer size used by the platform for output on this DatagramSocket.
|
int |
getSoTimeout() |
Retrieve setting for SO_TIMEOUT.
|
int |
getTrafficClass() |
Gets traffic class or type-of-service in the IP datagram
header for packets sent from this DatagramSocket.
|
boolean |
isBound() |
Returns the binding state of the socket.
|
boolean |
isClosed() |
Returns whether the socket is closed or not.
|
boolean |
isConnected() |
Returns the connection state of the socket.
|
void |
receive(DatagramPacket p) |
Receives a datagram packet from this socket.
|
void |
send(DatagramPacket p) |
Sends a datagram packet from this socket.
|
void |
setBroadcast(boolean on) |
Enable/disable SO_BROADCAST.
|
static void |
setDatagramSocketImplFactory(DatagramSocketImplFactory fac) |
Sets the datagram socket implementation factory for the
application.
|
void |
setReceiveBufferSize(int size) |
Sets the SO_RCVBUF option to the specified value for this
DatagramSocket.
|
void |
setReuseAddress(boolean on) |
Enable/disable the SO_REUSEADDR socket option.
|
void |
setSendBufferSize(int size) |
Sets the SO_SNDBUF option to the specified value for this
DatagramSocket.
|
void |
setSoTimeout(int timeout) |
Enable/disable SO_TIMEOUT with the specified timeout, in
milliseconds.
|
void |
setTrafficClass(int tc) |
Sets traffic class or type-of-service octet in the IP
datagram header for datagrams sent from this DatagramSocket.
|
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
isBound
public boolean isBound()
Returns the binding state of the socket.
If the socket was bound prior to being closed,
then this method will continue to return true
after the socket is closed.
- Returns:
- true if the socket successfully bound to an address
- Since:
- 1.4
-
getPort
public int getPort()
Returns the port number to which this socket is connected.
Returns -1 if the socket is not connected.
If the socket was connected prior to being closed,
then this method will continue to return the connected port number
after the socket is closed.
- Returns:
- the port number to which this socket is connected.
-
setSoTimeout
public void setSoTimeout(int timeout)
throws SocketException
Enable/disable SO_TIMEOUT with the specified timeout, in
milliseconds. With this option set to a non-zero timeout,
a call to receive() for this DatagramSocket
will block for only this amount of time. If the timeout expires,
a java.net.SocketTimeoutException is raised, though the
DatagramSocket is still valid. The option must be enabled
prior to entering the blocking operation to have effect. The
timeout must be > 0.
A timeout of zero is interpreted as an infinite timeout.
- Parameters:
timeout - the specified timeout in milliseconds.
- Throws:
SocketException - if there is an error in the underlying protocol, such as an UDP error.
- Since:
- JDK1.1
- See Also:
getSoTimeout()
-
getSoTimeout
public int getSoTimeout()
throws SocketException
Retrieve setting for SO_TIMEOUT. 0 returns implies that the
option is disabled (i.e., timeout of infinity).
- Returns:
- the setting for SO_TIMEOUT
- Throws:
SocketException - if there is an error in the underlying protocol, such as an UDP error.
- Since:
- JDK1.1
- See Also:
setSoTimeout(int)
-
getSendBufferSize
public int getSendBufferSize()
throws SocketException
Get value of the SO_SNDBUF option for this DatagramSocket, that is the
buffer size used by the platform for output on this DatagramSocket.
- Returns:
- the value of the SO_SNDBUF option for this
DatagramSocket
- Throws:
SocketException - if there is an error in
the underlying protocol, such as an UDP error.
- See Also:
setSendBufferSize(int)
-
getReceiveBufferSize
public int getReceiveBufferSize()
throws SocketException
Get value of the SO_RCVBUF option for this DatagramSocket, that is the
buffer size used by the platform for input on this DatagramSocket.
- Returns:
- the value of the SO_RCVBUF option for this
DatagramSocket
- Throws:
SocketException - if there is an error in the underlying protocol, such as an UDP error.
- See Also:
setReceiveBufferSize(int)
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|