Package totalcross.net
Class ServerSocket
- java.lang.Object
-
- totalcross.net.ServerSocket
-
public class ServerSocket extends java.lang.ObjectServerSocket is a TCP/IP network server socket.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.
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BACKLOGThe default backlog valuestatic intDEFAULT_SOTIMEOUTThe default value for the accept operation timeout.static intWAIT_FOREVERPassing a value of 0 to the constructor causes any accept operation to wait indefinitely.
-
Constructor Summary
Constructors Modifier Constructor Description protectedServerSocket()For internal use onlyServerSocket(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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Socketaccept()Listens for a connection to be made to this socket and accepts it, returning a Socket representing the connection established with the client.voidclose()Closes the server socket.protected voidfinalize()java.lang.StringgetHost()Returns the local address of this server socket.intgetLocalPort()Returns the local TCP port on which this socket is listening, passed in the constructor.
-
-
-
Field Detail
-
DEFAULT_SOTIMEOUT
public static final int DEFAULT_SOTIMEOUT
The default value for the accept operation timeout.- See Also:
- Constant Field Values
-
DEFAULT_BACKLOG
public static final int DEFAULT_BACKLOG
The default backlog value- See Also:
- Constant Field Values
-
WAIT_FOREVER
public static final int WAIT_FOREVER
Passing a value of 0 to the constructor causes any accept operation to wait indefinitely.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ServerSocket
protected ServerSocket()
For internal use only
-
ServerSocket
public ServerSocket(int port) throws IOExceptionOpens a server socket. This method establishes a server socket connection at the specified port, with the default timeout for accept operations.- Parameters:
port- the local TCP port to listen for incoming connections- Throws:
IOException
-
ServerSocket
public ServerSocket(int port, int timeout) throws IOExceptionOpens a server socket. This method establishes a server socket connection at the specified port, with the specified timeout for accept operations.- Parameters:
port- the local TCP port to listen for incoming connectionstimeout- the accept operation timeout- Throws:
IOException
-
ServerSocket
public ServerSocket(int port, int timeout, java.lang.String addr) throws IOExceptionOpens a server socket. This method establishes a server socket connection at the specified port, with the specified timeout for accept operations. The addr argument can be used on a multi-homed host for a ServerSocket that will only accept connect requests to one of its addresses.- Parameters:
port- the local TCP port to listen for incoming connectionstimeout- the accept operation timeoutaddr- the local address this server will bind to- Throws:
IOException
-
ServerSocket
public ServerSocket(int port, int timeout, int backlog, java.lang.String addr) throws IOExceptionOpens a server socket. This method establishes a server socket connection at the specified port, with the specified timeout for accept operations. The addr argument can be used on a multi-homed host for a ServerSocket that will only accept connect requests to one of its addresses.- Parameters:
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 to- Throws:
IOException
-
-
Method Detail
-
getHost
public java.lang.String getHost()
Returns the local address of this server socket.- Returns:
- the address to which this socket is bound, or null if the socket is unbound.
-
getLocalPort
public int getLocalPort()
Returns the local TCP port on which this socket is listening, passed in the constructor.- Returns:
- the port number to which this socket is listening.
-
accept
public Socket accept() throws IOException
Listens for a connection to be made to this socket and accepts it, returning a Socket representing the connection established with the client. This method blocks until a connection is made or the operation times out.- Returns:
- the client Socket
- Throws:
IOException
-
close
public void close() throws IOExceptionCloses the server socket.- Throws:
IOException
-
finalize
protected void finalize()
- Overrides:
finalizein classjava.lang.Object
-
-