java.net.DatagramSocket
This class models a socket for sending & receiving datagram packets.
Known Direct Subclasses
MulticastSocket |
This class models a multicast socket for sending & receiving datagram packets
to a multicast group. |
Summary
Public Constructors
Protected Constructors
Public Methods
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Public Constructors
public
DatagramSocket()
Constructs a datagram socket, bound to any available port on the
localhost.
public
DatagramSocket(int aPort)
Returns a datagram socket, bound to the nominated port on the localhost.
Parameters
aPort
| the port to bind on the localhost |
public
DatagramSocket(int aPort, InetAddress addr)
Constructs a datagram socket, bound to the nominated localhost/port.
Parameters
aPort
| the port on the localhost to bind |
addr
| the address on the multihomed localhost to bind |
public
DatagramSocket(SocketAddress localAddr)
Constructs a DatagramSocket bound to the host/port specified by the
SocketAddress, or an unbound DatagramSocket if the SocketAddress is null.
Parameters
localAddr
| the local machine address and port to bind to |
Protected Constructors
protected
DatagramSocket(DatagramSocketImpl socketImpl)
Constructs a DatagramSocket using the specified DatagramSocketImpl. The
DatagramSocket is not bound.
Parameters
socketImpl
| the DatagramSocketImpl to use
|
Public Methods
public
void
bind(SocketAddress localAddr)
Bind the DatagramSocket to the nominated local host/port.
Parameters
localAddr
| the local machine address and port to bind on |
public
void
close()
Close the socket.
public
void
connect(InetAddress anAddress, int aPort)
Connect the datagram socket to a remote host and port. The host and port
are validated, thereafter the only validation on send() and receive() is
that the packet address/port matches the connected target.
Parameters
anAddress
| the target address |
aPort
| the target port
|
public
void
connect(SocketAddress remoteAddr)
Connect the datagram socket to a remote host and port. The host and port
are validated, thereafter the only validation on send() and receive() is
that the packet address/port matches the connected target.
Parameters
remoteAddr
| the target address and port |
public
void
disconnect()
'Disconnect' the datagram socket from a remote host and port. This method
may be called on an unconnected socket.
public
boolean
getBroadcast()
Get the state of the SO_BROADCAST socket option.
Returns
true
if the SO_BROADCAST is enabled,
false
otherwise.
if DatagramSocket is created by a DatagramChannel, returns the related
DatagramChannel
Returns
- the related DatagramChannel if any
public
InetAddress
getInetAddress()
Returns an
InetAddress instance representing the address this
socket has connected to.
Returns
- if this socket is connected, the address it is connected to. A
null
return signifies no connection has been made.
public
InetAddress
getLocalAddress()
Returns an
InetAddress instance representing the
local
address this socket is bound to.
Returns
- the local address to which the socket is bound
public
int
getLocalPort()
Answer the local port to which the socket is bound.
Returns
- int local port to which the socket is bound
public
SocketAddress
getLocalSocketAddress()
Answer the local SocketAddress for this socket, or null if the socket is
not bound.
This is useful on multihomed hosts.
public
int
getPort()
Returns the number of the remote port this socket is connected to.
Returns
- int the remote port number that this socket has connected to. A
return of
-1
indicates that there is no connection
in place.
public
synchronized
int
getReceiveBufferSize()
Answer the socket receive buffer size (SO_RCVBUF).
Returns
- int socket receive buffer size
public
SocketAddress
getRemoteSocketAddress()
Answer the remote SocketAddress for this socket, or null if the socket is
not connected.
Returns
- the remote socket address
public
boolean
getReuseAddress()
Get the state of the SO_REUSEADDR socket option.
Returns
true
if the SO_REUSEADDR is enabled,
false
otherwise.
public
synchronized
int
getSendBufferSize()
Answer the socket send buffer size (SO_SNDBUF).
Returns
- int socket send buffer size
public
synchronized
int
getSoTimeout()
Answer the socket receive timeout (SO_RCVTIMEOUT), in milliseconds. Zero
implies the timeout is disabled.
Returns
- int socket receive timeout
public
int
getTrafficClass()
Get the IP_TOS socket option.
Returns
- the IP_TOS socket option value
public
boolean
isBound()
Return if the socket is bound to a local address and port.
Returns
true
if the socket is bound to a local address,
false
otherwise.
public
boolean
isClosed()
Return if the socket is closed.
Returns
true
if the socket is closed, false
otherwise.
public
boolean
isConnected()
Return if the socket is connected.
Returns
true
if the socket is connected,
false
otherwise.
public
synchronized
void
receive(DatagramPacket pack)
Receive on this socket into the packet argument. This method blocks until
a packet is received or, if a timeout has been defined, the timeout
period expires. If this is a connected socket, the packet host/port are
compared to the connection host/port otherwise the security manager if
present is queried whether the packet host/port is acceptable. Any
packets from unacceptable origins will be silently discarded. The packet
fields are set according to the data received. If the received data is
longer than the packet buffer, it is truncated.
Parameters
pack
| the DatagramPacket to receive data into |
Send the packet on this socket. The packet must satisfy the security
policy before it may be sent.
Parameters
pack
| the DatagramPacket to send |
public
void
setBroadcast(boolean broadcast)
Set the SO_BROADCAST socket option.
Parameters
broadcast
| the socket SO_BROADCAST option setting |
public
static
synchronized
void
setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
Specifies the application's socket implementation factory. This may only
be invoked once over the lifetime of the application.
Parameters
fac
| the socket factory to set |
public
synchronized
void
setReceiveBufferSize(int size)
Set the socket receive buffer size.
Parameters
size
| the buffer size, in bytes. Must be at least one byte. |
Throws
SocketException
| If an error occurs while setting the size or the size is
invalid.
|
public
void
setReuseAddress(boolean reuse)
Set the SO_REUSEADDR socket option.
Parameters
reuse
| the socket SO_REUSEADDR option setting |
public
synchronized
void
setSendBufferSize(int size)
Set the socket send buffer size.
Parameters
size
| the buffer size, in bytes. Must be at least one byte. |
Throws
SocketException
| If an error occurs while setting the size or the size is
invalid.
|
public
synchronized
void
setSoTimeout(int timeout)
Set the SO_RCVTIMEOUT to
timeout
, in milliseconds. The
receive timeout defines the period a socket will block waiting to receive
data, before throwing an InterruptedIOException.
Parameters
timeout
| the timeout period, in milliseconds |
Throws
SocketException
| If an error occurs while setting the timeout or the period
is invalid.
|
public
void
setTrafficClass(int value)
Set the IP_TOS socket option.
Parameters
value
| the socket IP_TOS setting |