public class ServerSocket
extends java.lang.Object
Under Java and Windows CE, if no network is present, the socket constructor may hang for an extended period of time due to the implementation of sockets in the underlying OS. This is a known problem.
Here is an example showing the server accepting a client connection and writing/reading some data to/from it:
ServerSocket server = new ServerSocket(1024); Socket client = server.accept(); byte[] bytes = "HELLO WORLD".getBytes(); client.writeBytes(bytes, 0, bytes.length); byte[] buf = new byte[10]; int count = client.readBytes(buf, 0, buf.length); if (count == buf.length) ... client.close(); server.close();Important: you cannot open a server socket before the main event loop. In other words, you cannot open a socket in the app's constructor, but CAN in the initUI method.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BACKLOG
The default backlog value
|
static int |
DEFAULT_SOTIMEOUT
The default value for the accept operation timeout.
|
static int |
WAIT_FOREVER
Passing a value of 0 to the constructor causes any accept operation to wait indefinitely.
|
Modifier | Constructor and Description |
---|---|
protected |
ServerSocket()
For internal use only
|
|
ServerSocket(int port)
Opens a server socket.
|
|
ServerSocket(int port,
int timeout)
Opens a server socket.
|
|
ServerSocket(int port,
int timeout,
int backlog,
java.lang.String addr)
Opens a server socket.
|
|
ServerSocket(int port,
int timeout,
java.lang.String addr)
Opens a server socket.
|
Modifier and Type | Method and Description |
---|---|
Socket |
accept()
Listens for a connection to be made to this socket and accepts it, returning
a Socket representing the connection established with the client.
|
void |
close()
Closes the server socket.
|
protected void |
finalize() |
java.lang.String |
getHost()
Returns the local address of this server socket.
|
int |
getLocalPort()
Returns the local TCP port on which this socket is listening, passed in the constructor.
|
public static final int DEFAULT_SOTIMEOUT
public static final int DEFAULT_BACKLOG
public static final int WAIT_FOREVER
protected ServerSocket()
public ServerSocket(int port) throws IOException
port
- the local TCP port to listen for incoming connectionsIOException
public ServerSocket(int port, int timeout) throws IOException
port
- the local TCP port to listen for incoming connectionstimeout
- the accept operation timeoutIOException
public ServerSocket(int port, int timeout, java.lang.String addr) throws IOException
port
- the local TCP port to listen for incoming connectionstimeout
- the accept operation timeoutaddr
- the local address this server will bind toIOException
public ServerSocket(int port, int timeout, int backlog, java.lang.String addr) throws IOException
port
- the local TCP port to listen for incoming connectionstimeout
- the accept operation timeoutbacklog
- the limit of concurrent connections that are accepted.addr
- the local address this server will bind toIOException
public java.lang.String getHost()
public int getLocalPort()
public Socket accept() throws IOException
IOException
public void close() throws IOException
IOException
protected void finalize()
finalize
in class java.lang.Object