Methods Summary |
---|
public void | bind(java.net.SocketAddress localAddr)Binds this socket to the local address and port specified by {@code
localAddr}. If this value is {@code null} any free port on a valid local
address is used.
checkClosedAndBind(false);
int localPort = 0;
InetAddress addr = InetAddress.ANY;
if (localAddr != null) {
if (!(localAddr instanceof InetSocketAddress)) {
throw new IllegalArgumentException(Msg.getString(
"K0316", localAddr.getClass())); //$NON-NLS-1$
}
InetSocketAddress inetAddr = (InetSocketAddress) localAddr;
addr = inetAddr.getAddress();
if (addr == null) {
throw new SocketException(Msg.getString(
"K0317", inetAddr.getHostName())); //$NON-NLS-1$
}
localPort = inetAddr.getPort();
checkListen(localPort);
}
impl.bind(localPort, addr);
isBound = true;
|
void | checkClosedAndBind(boolean bind)
if (isClosed()) {
throw new SocketException(Msg.getString("K003d")); //$NON-NLS-1$
}
if (bind && !isBound()) {
checkListen(0);
impl.bind(0, InetAddress.ANY);
isBound = true;
}
|
void | checkListen(int aPort)Sends prior to attempting to bind the socket, checks whether the port is
within the valid port range and verifies with the security manager that
the port may be bound by the current context.
if (aPort < 0 || aPort > 65535) {
throw new IllegalArgumentException(Msg.getString("K0325", aPort)); //$NON-NLS-1$
}
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkListen(aPort);
}
|
public void | close()Closes this UDP datagram socket and all possibly associated channels.
isClosed = true;
impl.close();
|
public void | connect(java.net.SocketAddress remoteAddr)Connects this datagram socket to the remote host and port specified by
{@code remoteAddr}. The host and port are validated, thereafter the only
validation on {@code send()} and {@code receive()} is that the packet
address/port matches the connected target.
if (remoteAddr == null) {
throw new IllegalArgumentException(Msg.getString("K0318")); //$NON-NLS-1$
}
if (!(remoteAddr instanceof InetSocketAddress)) {
throw new IllegalArgumentException(Msg.getString(
"K0316", remoteAddr.getClass())); //$NON-NLS-1$
}
InetSocketAddress inetAddr = (InetSocketAddress) remoteAddr;
if (inetAddr.getAddress() == null) {
throw new SocketException(Msg.getString(
"K0317", inetAddr.getHostName())); //$NON-NLS-1$
}
synchronized (lock) {
// make sure the socket is open
checkClosedAndBind(true);
SecurityManager security = System.getSecurityManager();
if (security != null) {
if (inetAddr.getAddress().isMulticastAddress()) {
security.checkMulticast(inetAddr.getAddress());
} else {
security.checkConnect(inetAddr.getAddress().getHostName(),
inetAddr.getPort());
}
}
// now try to do the connection at the native level. To be
// compatible for the case when the address is inaddr_any we just
// eat the exception an act as if we are connected at the java level
try {
impl.connect(inetAddr.getAddress(), inetAddr.getPort());
} catch (Exception e) {
// not connected at the native level just do what we did before
}
// if we get here then we connected ok
address = inetAddr.getAddress();
port = inetAddr.getPort();
isConnected = true;
}
|
public void | connect(java.net.InetAddress anAddress, int aPort)Connects this UDP datagram socket to the specific target host with the
address {@code anAdress} on port {@code aPort}. The host and port are
validated, thereafter the only validation on {@code send()} and {@code
receive()} is to check whether the packet address/port matches the
connected target.
if (anAddress == null || aPort < 0 || aPort > 65535) {
throw new IllegalArgumentException(Msg.getString("K0032")); //$NON-NLS-1$
}
synchronized (lock) {
if (isClosed()) {
return;
}
try {
checkClosedAndBind(true);
} catch (SocketException e) {
// Ignored
}
SecurityManager security = System.getSecurityManager();
if (security != null) {
if (anAddress.isMulticastAddress()) {
security.checkMulticast(anAddress);
} else {
security.checkConnect(anAddress.getHostName(), aPort);
}
}
try {
impl.connect(anAddress, aPort);
} catch (SocketException e) {
// not connected at the native level just do what we did before
}
address = anAddress;
port = aPort;
isConnected = true;
}
|
synchronized void | createSocket(int aPort, java.net.InetAddress addr)
impl = factory != null ? factory.createDatagramSocketImpl()
: SocketImplProvider.getDatagramSocketImpl();
impl.create();
try {
impl.bind(aPort, addr);
isBound = true;
} catch (SocketException e) {
close();
throw e;
}
|
public void | disconnect()Disconnects this UDP datagram socket from the remote host. This method
called on an unconnected socket does nothing.
if (isClosed() || !isConnected()) {
return;
}
impl.disconnect();
address = null;
port = -1;
isConnected = false;
|
public boolean | getBroadcast()Gets the state of the socket option {@code SocketOptions.SO_BROADCAST}.
checkClosedAndBind(false);
return ((Boolean) impl.getOption(SocketOptions.SO_BROADCAST))
.booleanValue();
|
public java.nio.channels.DatagramChannel | getChannel()Gets the related DatagramChannel of this socket. This implementation
returns always {@code null}.
return null;
|
public java.net.InetAddress | getInetAddress()Gets the {@code InetAddress} instance representing the remote address to
which this UDP datagram socket is connected.
return address;
|
public java.net.InetAddress | getLocalAddress()Gets the {@code InetAddress} instance representing the bound local
address of this UDP datagram socket.
if (isClosed()) {
return null;
}
if (!isBound()) {
return InetAddress.ANY;
}
InetAddress anAddr = impl.getLocalAddress();
try {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkConnect(anAddr.getHostName(), -1);
}
} catch (SecurityException e) {
return InetAddress.ANY;
}
return anAddr;
|
public int | getLocalPort()Gets the local port which this socket is bound to.
if (isClosed()) {
return -1;
}
if (!isBound()) {
return 0;
}
return impl.getLocalPort();
|
public java.net.SocketAddress | getLocalSocketAddress()Gets the bound local address and port of this socket. If the socket is
unbound, {@code null} is returned.
if (!isBound()) {
return null;
}
return new InetSocketAddress(getLocalAddress(), getLocalPort());
|
public int | getPort()Gets the remote port which this socket is connected to.
return port;
|
public synchronized int | getReceiveBufferSize()Gets the socket receive buffer size. ( {@code SocketOptions.SO_RCVBUF} )
checkClosedAndBind(false);
return ((Integer) impl.getOption(SocketOptions.SO_RCVBUF)).intValue();
|
public java.net.SocketAddress | getRemoteSocketAddress()Gets the address and port of the connected remote host. If this socket is
not connected yet, {@code null} is returned.
if (!isConnected()) {
return null;
}
return new InetSocketAddress(getInetAddress(), getPort());
|
public boolean | getReuseAddress()Gets the state of the socket option {@code SocketOptions.SO_REUSEADDR}.
checkClosedAndBind(false);
return ((Boolean) impl.getOption(SocketOptions.SO_REUSEADDR))
.booleanValue();
|
public synchronized int | getSendBufferSize()Gets the socket send buffer size. ( {@code SocketOptions.SO_SNDBUF} )
checkClosedAndBind(false);
return ((Integer) impl.getOption(SocketOptions.SO_SNDBUF)).intValue();
|
public synchronized int | getSoTimeout()Gets the socket receive timeout in milliseconds. The return value {@code
0} implies the timeout is disabled/infinitive. ( {@code
SocketOptions.SO_TIMEOUT} )
checkClosedAndBind(false);
return ((Integer) impl.getOption(SocketOptions.SO_TIMEOUT)).intValue();
|
public int | getTrafficClass()Gets the value of the type-of-service socket option {@code
SocketOptions.IP_TOS}.
checkClosedAndBind(false);
return ((Number) impl.getOption(SocketOptions.IP_TOS)).intValue();
|
public boolean | isBound()Determines whether the socket is bound to an address or not.
return isBound;
|
public boolean | isClosed()Gets the state of this socket.
return isClosed;
|
public boolean | isConnected()Determines whether the socket is connected to a target host.
return isConnected;
|
boolean | isMulticastSocket()Indicates whether this socket is multicast or not.
return false;
|
public synchronized void | receive(java.net.DatagramPacket pack)Receives a packet from this socket and stores it in the argument {@code
pack}. All fields of {@code pack} must be set according to the data
received. If the received data is longer than the packet buffer size it
is truncated. This method blocks until a packet is received or a timeout
has expired. If a security manager exists, its {@code checkAccept} method
determines whether or not a packet is discarded. Any packets from
unacceptable origins are silently discarded.
checkClosedAndBind(true);
boolean secure = true;
InetAddress senderAddr = null;
int senderPort = 0;
DatagramPacket tempPack = new DatagramPacket(new byte[1], 1);
boolean copy = false;
SecurityManager security = System.getSecurityManager();
if (address != null || security != null) { // The socket is connected
// Check pack before peeking
if (pack == null) {
throw new NullPointerException();
}
secure = false;
while (!secure) {
copy = false;
try {
senderPort = impl.peekData(tempPack);
senderAddr = tempPack.getAddress();
} catch (SocketException e) {
if (e.getMessage().equals(
"The socket does not support the operation")) { //$NON-NLS-1$
tempPack = new DatagramPacket(new byte[pack.length],
pack.getLength());
impl.receive(tempPack);
senderAddr = tempPack.getAddress();
senderPort = tempPack.getPort();
copy = true;
} else {
throw e;
}
}
if (address == null) {
try {
security.checkAccept(senderAddr.getHostName(),
senderPort);
if (!copy) {
secure = true;
}
break;
} catch (SecurityException e) {
if (!copy) {
if (tempPack == null) {
tempPack = new DatagramPacket(
new byte[pack.length], pack.length);
}
impl.receive(tempPack);
}
}
} else if (port == senderPort && address.equals(senderAddr)) {
if (!copy) {
secure = true;
}
break;
} else if (!copy) {
if (tempPack == null) {
tempPack = new DatagramPacket(new byte[pack.length],
pack.length);
}
impl.receive(tempPack);
}
}
}
if (copy) {
System.arraycopy(tempPack.getData(), 0, pack.getData(), pack
.getOffset(), tempPack.getLength());
pack.setLength(tempPack.getLength());
pack.setAddress(tempPack.getAddress());
pack.setPort(tempPack.getPort());
}
if (secure) {
impl.receive(pack);
}
|
public void | send(java.net.DatagramPacket pack)Sends a packet over this socket. The packet must satisfy the security
policy before it may be sent. If a security manager is installed, this
method checks whether it is allowed to send this packet to the specified
address.
checkClosedAndBind(true);
InetAddress packAddr = pack.getAddress();
if (address != null) { // The socket is connected
if (packAddr != null) {
if (!address.equals(packAddr) || port != pack.getPort()) {
throw new IllegalArgumentException(Msg.getString("K0034")); //$NON-NLS-1$
}
} else {
pack.setAddress(address);
pack.setPort(port);
}
} else {
// not connected so the target address is not allowed to be null
if (packAddr == null) {
if (pack.port == -1) {
// KA019 Destination address is null
throw new NullPointerException(Msg.getString("KA019")); //$NON-NLS-1$
}
return;
}
SecurityManager security = System.getSecurityManager();
if (security != null) {
if (packAddr.isMulticastAddress()) {
security.checkMulticast(packAddr);
} else {
security.checkConnect(packAddr.getHostName(), pack
.getPort());
}
}
}
impl.send(pack);
|
public void | setBroadcast(boolean broadcast)Sets the socket option {@code SocketOptions.SO_BROADCAST}. This option
must be enabled to send broadcast messages.
checkClosedAndBind(false);
impl.setOption(SocketOptions.SO_BROADCAST, broadcast ? Boolean.TRUE
: Boolean.FALSE);
|
public static synchronized void | setDatagramSocketImplFactory(java.net.DatagramSocketImplFactory fac)Sets the socket implementation factory. This may only be invoked once
over the lifetime of the application. This factory is used to create
a new datagram socket implementation. If a security manager is set its
method {@code checkSetFactory()} is called to check if the operation is
allowed. A {@code SecurityException} is thrown if the operation is not
allowed.
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkSetFactory();
}
if (factory != null) {
throw new SocketException(Msg.getString("K0044")); //$NON-NLS-1$
}
factory = fac;
|
public synchronized void | setReceiveBufferSize(int size)Sets the socket receive buffer size. This buffer size determines which
the maximum packet size is that can be received over this socket. It
depends on the network implementation what will happen if the packet is
bigger than the buffer size. ( {@code SocketOptions.SO_RCVBUF} )
if (size < 1) {
throw new IllegalArgumentException(Msg.getString("K0035")); //$NON-NLS-1$
}
checkClosedAndBind(false);
impl.setOption(SocketOptions.SO_RCVBUF, Integer.valueOf(size));
|
public void | setReuseAddress(boolean reuse)Sets the socket option {@code SocketOptions.SO_REUSEADDR}. This option
has to be enabled if more than one UDP socket wants to be bound to the
same address. That could be needed for receiving multicast packets.
There is an undefined behavior if this option is set after the socket is
already bound.
checkClosedAndBind(false);
impl.setOption(SocketOptions.SO_REUSEADDR, reuse ? Boolean.TRUE
: Boolean.FALSE);
|
public synchronized void | setSendBufferSize(int size)Sets the socket send buffer size. This buffer size determines which the
maximum packet size is that can be sent over this socket. It depends on
the network implementation what will happen if the packet is bigger than
the buffer size. ( {@code SocketOptions.SO_SNDBUF} )
if (size < 1) {
throw new IllegalArgumentException(Msg.getString("K0035")); //$NON-NLS-1$
}
checkClosedAndBind(false);
impl.setOption(SocketOptions.SO_SNDBUF, Integer.valueOf(size));
|
public synchronized void | setSoTimeout(int timeout)Sets the timeout period in milliseconds for the {@code receive()} method.
This receive timeout defines the period the socket will block waiting to
receive data before throwing an {@code InterruptedIOException}. The value
{@code 0} (default) is used to set an infinite timeout. To have effect
this option must be set before the blocking method was called. ( {@code
SocketOptions.SO_TIMEOUT} )
if (timeout < 0) {
throw new IllegalArgumentException(Msg.getString("K0036")); //$NON-NLS-1$
}
checkClosedAndBind(false);
impl.setOption(SocketOptions.SO_TIMEOUT, Integer.valueOf(timeout));
|
public void | setTrafficClass(int value)Sets the socket option {@code SocketOptions.IP_TOS}. This option defines
the value of the type-of-service field of the IP-header for every packet
sent by this socket. The value could be ignored by the underlying network
implementation.
Values between {@code 0} and {@code 255} inclusive are valid for this
option.
checkClosedAndBind(false);
if (value < 0 || value > 255) {
throw new IllegalArgumentException();
}
impl.setOption(SocketOptions.IP_TOS, Integer.valueOf(value));
|