Constructors Summary |
---|
public ServerSocket()Constructs a new {@code ServerSocket} instance which is not bound to any
port. The default number of pending connections may be backlogged.
Platform.getNetworkSystem().oneTimeInitialization(true);
impl = factory != null ? factory.createSocketImpl()
: SocketImplProvider.getServerSocketImpl();
|
protected ServerSocket(SocketImpl impl)Unspecified constructor.
Warning: this function is technically part of API#1.
Hiding it for API#2 broke source compatibility.
Removing it entirely would theoretically break binary compatibility,
and would be better done with some visibility over the extent
of the compatibility breakage (expected to be non-existent).
this.impl = impl;
|
public ServerSocket(int aport)Constructs a new {@code ServerSocket} instance bound to the nominated
port on the localhost. The default number of pending connections may be
backlogged. If {@code aport} is 0 a free port is assigned to the socket.
this(aport, defaultBacklog(), InetAddress.ANY);
|
public ServerSocket(int aport, int backlog)Constructs a new {@code ServerSocket} instance bound to the nominated
port on the localhost. The number of pending connections that may be
backlogged is specified by {@code backlog}. If {@code aport} is 0 a free
port is assigned to the socket.
this(aport, backlog, InetAddress.ANY);
|
public ServerSocket(int aport, int backlog, InetAddress localAddr)Constructs a new {@code ServerSocket} instance bound to the nominated
local host address and port. The number of pending connections that may
be backlogged is specified by {@code backlog}. If {@code aport} is 0 a
free port is assigned to the socket.
super();
checkListen(aport);
impl = factory != null ? factory.createSocketImpl()
: SocketImplProvider.getServerSocketImpl();
InetAddress addr = localAddr == null ? InetAddress.ANY : localAddr;
synchronized (this) {
impl.create(true);
isCreated = true;
try {
impl.bind(addr, aport);
isBound = true;
impl.listen(backlog > 0 ? backlog : defaultBacklog());
} catch (IOException e) {
close();
throw e;
}
}
|
Methods Summary |
---|
public java.net.Socket | accept()Waits for an incoming request and blocks until the connection is opened.
This method returns a socket object representing the just opened
connection.
checkClosedAndCreate(false);
if (!isBound()) {
throw new SocketException(Msg.getString("K031f")); //$NON-NLS-1$
}
Socket aSocket = new Socket();
try {
synchronized (this) {
implAccept(aSocket);
}
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkAccept(aSocket.getInetAddress().getHostAddress(),
aSocket.getPort());
}
} catch (SecurityException e) {
aSocket.close();
throw e;
} catch (IOException e) {
aSocket.close();
throw e;
}
return aSocket;
|
public void | bind(java.net.SocketAddress localAddr)Binds this server socket to the given local socket address. The default
number of pending connections may be backlogged. If the {@code localAddr}
is set to {@code null} the socket will be bound to an available local
address on any free port of the system.
bind(localAddr, defaultBacklog());
|
public void | bind(java.net.SocketAddress localAddr, int backlog)Binds this server socket to the given local socket address. If the
{@code localAddr} is set to {@code null} the socket will be bound to an
available local address on any free port of the system. The value for
{@code backlog} must e greater than {@code 0} otherwise the default value
will be used.
checkClosedAndCreate(true);
if (isBound()) {
throw new BindException(Msg.getString("K0315")); //$NON-NLS-1$
}
int port = 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;
if ((addr = inetAddr.getAddress()) == null) {
throw new SocketException(Msg.getString(
"K0317", inetAddr.getHostName())); //$NON-NLS-1$
}
port = inetAddr.getPort();
}
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkListen(port);
}
synchronized (this) {
try {
impl.bind(addr, port);
isBound = true;
impl.listen(backlog > 0 ? backlog : defaultBacklog());
} catch (IOException e) {
close();
throw e;
}
}
|
private void | checkClosedAndCreate(boolean create)Checks whether the socket is closed, and throws an exception.
if (isClosed()) {
throw new SocketException(Msg.getString("K003d")); //$NON-NLS-1$
}
if (!create || isCreated) {
return;
}
synchronized (this) {
if (isCreated) {
return;
}
try {
impl.create(true);
} catch (SocketException e) {
throw e;
} catch (IOException e) {
throw new SocketException(e.toString());
}
isCreated = true;
}
|
void | checkListen(int aPort)Checks whether the server may listen for connection requests on {@code
aport}. Throws an exception if the port is outside the valid range
{@code 0 <= aport <= 65535 }or does not satisfy the security policy.
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 server socket and its implementation. Any attempt to connect
to this socket thereafter will fail.
isClosed = true;
impl.close();
|
static int | defaultBacklog()Answer the default number of pending connections on a server socket. If
the backlog value maximum is reached, any subsequent incoming request is
rejected.
return 50;
|
public java.nio.channels.ServerSocketChannel | getChannel()Gets the related channel if this instance was created by a
{@code ServerSocketChannel}. The current implementation returns always {@code
null}.
return null;
|
public java.net.InetAddress | getInetAddress()Gets the local IP address of this server socket or {@code null} if the
socket is unbound. This is useful for multihomed hosts.
if (!isBound()) {
return null;
}
return impl.getInetAddress();
|
public int | getLocalPort()Gets the local port of this server socket or {@code -1} if the socket is
unbound.
if (!isBound()) {
return -1;
}
return impl.getLocalPort();
|
public java.net.SocketAddress | getLocalSocketAddress()Gets the local socket address of this server socket or {@code null} if
the socket is unbound. This is useful on multihomed hosts.
if (!isBound()) {
return null;
}
return new InetSocketAddress(getInetAddress(), getLocalPort());
|
public int | getReceiveBufferSize()Gets the value for the receive buffer size socket option {@code
SocketOptions.SO_RCVBUF}.
checkClosedAndCreate(true);
return ((Integer) impl.getOption(SocketOptions.SO_RCVBUF)).intValue();
|
public boolean | getReuseAddress()Gets the value of the socket option {@code SocketOptions.SO_REUSEADDR}.
checkClosedAndCreate(true);
return ((Boolean) impl.getOption(SocketOptions.SO_REUSEADDR))
.booleanValue();
|
public synchronized int | getSoTimeout()Gets the timeout period of this server socket. This is the time the
server will wait listening for accepted connections before exiting.
if (!isCreated) {
synchronized (this) {
if (!isCreated) {
try {
impl.create(true);
} catch (SocketException e) {
throw e;
} catch (IOException e) {
throw new SocketException(e.toString());
}
isCreated = true;
}
}
}
return ((Integer) impl.getOption(SocketOptions.SO_TIMEOUT)).intValue();
|
protected final void | implAccept(java.net.Socket aSocket)Invokes the server socket implementation to accept a connection on the
given socket {@code aSocket}.
impl.accept(aSocket.impl);
aSocket.accepted();
|
public boolean | isBound()Returns whether this server socket is bound to a local address and port
or not.
return isBound;
|
public boolean | isClosed()Returns whether this server socket is closed or not.
return isClosed;
|
public void | setPerformancePreferences(int connectionTime, int latency, int bandwidth)Sets performance preferences for connection time, latency and bandwidth.
This method does currently nothing.
// Our socket implementation only provide one protocol: TCP/IP, so
// we do nothing for this method
|
public void | setReceiveBufferSize(int size)Sets the server socket receive buffer size {@code
SocketOptions.SO_RCVBUF}.
checkClosedAndCreate(true);
if (size < 1) {
throw new IllegalArgumentException(Msg.getString("K0035")); //$NON-NLS-1$
}
impl.setOption(SocketOptions.SO_RCVBUF, Integer.valueOf(size));
|
public void | setReuseAddress(boolean reuse)Sets the value for the socket option {@code SocketOptions.SO_REUSEADDR}.
checkClosedAndCreate(true);
impl.setOption(SocketOptions.SO_REUSEADDR, reuse ? Boolean.TRUE
: Boolean.FALSE);
|
public synchronized void | setSoTimeout(int timeout)Sets the timeout period of this server socket. This is the time the
server will wait listening for accepted connections before exiting. This
value must be a positive number.
checkClosedAndCreate(true);
if (timeout < 0) {
throw new IllegalArgumentException(Msg.getString("K0036")); //$NON-NLS-1$
}
impl.setOption(SocketOptions.SO_TIMEOUT, Integer.valueOf(timeout));
|
public static synchronized void | setSocketFactory(java.net.SocketImplFactory aFactory)Sets the server socket implementation factory of this instance. This
method may only be invoked with sufficient security privilege and only
once during the application lifetime.
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkSetFactory();
}
if (factory != null) {
throw new SocketException(Msg.getString("K0042")); //$NON-NLS-1$
}
factory = aFactory;
|
public java.lang.String | toString()Returns a textual representation of this server socket including the
address, port and the state. The port field is set to {@code 0} if there
is no connection to the server socket.
StringBuffer result = new StringBuffer(64);
result.append("ServerSocket["); //$NON-NLS-1$
if (!isBound()) {
return result.append("unbound]").toString(); //$NON-NLS-1$
}
return result.append("addr=") //$NON-NLS-1$
.append(getInetAddress().getHostName()).append("/") //$NON-NLS-1$
.append(getInetAddress().getHostAddress()).append(
",port=0,localport=") //$NON-NLS-1$
.append(getLocalPort()).append("]") //$NON-NLS-1$
.toString();
|