A TCP server socket that keeps listening to new connections.
The server socket must be bound to a local port before it can start accepting new connections. Once a request is coming it will perform the three-way handshake with the client and creates a Socket instance upon the successful establishment of the connection. The ensuing message exchange and disconnection will be performed through Socket's API.
Parent Class
Parent Interfaces
Type | Name | Signature |
---|---|---|
constructor | ServerSocket | public ServerSocket() |
constructor | ServerSocket | public ServerSocket(System.Network.SocketConfig) |
constructor | ServerSocket | public ServerSocket(string, int) |
constructor | ServerSocket | public ServerSocket(int) |
method | accept | public Socket accept() |
method | bind | public void bind(string, int) |
method | close | public void close() |
method | getLocalHost | public String getLocalHost() |
method | getLocalPort | public int getLocalPort() |
method | getSetting | public var getSetting(System.Network.TCPOption) |
method | getState | public SocketState getState() |
public ServerSocket()
Create a new server socket with default configuration.
public ServerSocket(SocketConfig config)
Create a new server socket with given configuration.
Parameters
public ServerSocket(string localHost, int localPort)
Create a new server socket with given local host and port.
Parameters
public ServerSocket(int localPort)
Create a new server socket with given local port.
Parameters
public Socket accept()
Accept the next connection.
If no host and/or port has been specified in the constructor or explicitly designated in bind() method, this method will first bind to a the local host on an ephemeral port. Otherwise it will bind to whatever is given.
This method blocks until the next connection comes, when it creates a Socket instance for the application to consume. The subsequent communication on that socket no longer concerns the originating server socket. To keep the thread running for the subsequent connections, it's typical to invoke certain asynchronous methods on the socket's I/O streams and let it go to the next call on bind() immediately. Alternatively, one can also spawn a new thread to process the socket in a synchronous way, although such practices, common to traditional server programming, is not recommended in Julian, which prefers an async-first API model.
Required Policies
System.Socket/listen
Returns
public void bind(string localHost, int localPort)
Bind this server socket to a local address.
Required Policies
System.Socket/connect
Parameters
public void close()
Close the server socket. This will only spin down the listening operation. It will not affect any sockets created from this server socket.
public String getLocalHost()
Get the local host for this server socket. If binding has not happened yet, returns the value set in configuration.
Returns
public int getLocalPort()
Get the local port for this server socket. If binding has not happened yet, returns the value set in configuration.
Returns
public var getSetting(TCPOption opt)
Get the setting for specified TCP option.
Parameters
Returns
public SocketState getState()
Get the state of this server socket.
Note that the server socket's state always starts from UNBOUND, and can only transition into either BOUND through bind() or accept() call. It will remain bound until it's explicitly closed by closed(), upon which it becomes CLOSED or BOUND, if the local address has been explicitly specified and preserved.
Returns