ServerSocket CLASS

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

All Members


TypeNameSignature
constructorServerSocketpublic ServerSocket()
constructorServerSocketpublic ServerSocket(System.Network.SocketConfig)
constructorServerSocketpublic ServerSocket(string, int)
constructorServerSocketpublic ServerSocket(int)
methodacceptpublic Socket accept()
methodbindpublic void bind(string, int)
methodclosepublic void close()
methodgetLocalHostpublic String getLocalHost()
methodgetLocalPortpublic int getLocalPort()
methodgetSettingpublic var getSetting(System.Network.TCPOption)
methodgetStatepublic SocketState getState()

Constructors


public ServerSocket()

Create a new server socket with default configuration.


public ServerSocket(SocketConfig config)

Create a new server socket with given configuration.

Parameters

  • config Socket configuration

public ServerSocket(string localHost, int localPort)

Create a new server socket with given local host and port.

Parameters

  • localHost The local host name to bind to before starting listening.
  • localPort The local port to bind to before starting listening. 0 if using a ephemeral port.

public ServerSocket(int localPort)

Create a new server socket with given local port.

Parameters

  • localPort The local port to bind to before starting listening. 0 if using an ephemeral port.

Methods


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

  • The socket created as the result of next connection.

public void bind(string localHost, int localPort)

Bind this server socket to a local address.

Required Policies

  • System.Socket/connect

Parameters

  • localHost The local host name to bind to before starting listening.
  • localPort The local port to bind to before starting listening.

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

  • The local host bound to or to bind to this server socket. Null if no host has been specified yet.

public int getLocalPort()

Get the local port for this server socket. If binding has not happened yet, returns the value set in configuration.

Returns

  • The local port bound to or to bind to this server socket. 0 if no port has been specified yet.

public var getSetting(TCPOption opt)

Get the setting for specified TCP option.

Parameters

  • opt The TCP option to query.

Returns

  • The value set for this option. If the option is not set, the default value is returned. See TCPOption for more details on the type of each setting and the respective default values.

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

  • The state of this server socket.