Socket CLASS

A TCP socket.

A socket can be created in two ways: either by one of the constructors defined on this class, or by accepting a connection request from ServerSocket.

Once connected, the socket provides two streams for I/O operations. Closing either of them will result in disconnection.

Parent Class

Parent Interfaces

All Members


TypeNameSignature
constructorSocketpublic Socket()
constructorSocketpublic Socket(System.Network.SocketConfig)
constructorSocketpublic Socket(string, int)
constructorSocketpublic Socket(int)
methodbindpublic void bind(string, int)
methodclosepublic void close()
methodconnectpublic void connect(string, int)
methodgetLocalHostpublic String getLocalHost()
methodgetLocalPortpublic int getLocalPort()
methodgetReadStreampublic SocketStream getReadStream()
methodgetRemoteHostpublic String getRemoteHost()
methodgetRemotePortpublic int getRemotePort()
methodgetSettingpublic var getSetting(System.Network.TCPOption)
methodgetStatepublic SocketState getState()
methodgetWriteStreampublic SocketStream getWriteStream()

Constructors


public Socket()

Create a socket which is not bound yet.


public Socket(SocketConfig config)

Create a socket from a configuration object. Even if the object contains local address/port, the socket won't be bound to them until it initiates a connection.

Parameters

  • config The TCP configuration.

public Socket(string localHost, int localPort)

Create a socket from a configuration object. The socket won't be bound to the specified local address/port until it initiates a connection.

Parameters

  • localHost The local host name to bind to upon connection.
  • localPort The local port to bind to upon connection.

public Socket(int localPort)

Create a socket from a configuration object. The socket won't be bound to the specified local port until it initiates a connection.

Parameters

  • localPort The local port to bind to upon connection.

Methods


public void bind(string localHost, int localPort)

Bind this socket to a local address/port.

This has the effect of actually binding the socket to the specified address/port. Once the socket is bound, it cannot be rebound to another address/port, and it will remain in BOUND state forever, even after disconnection.

Required Policies

  • System.Socket/connect

Parameters

  • localHost The local host name to bind to upon connection.
  • localPort The local port to bind to upon connection.

public void close()

Close this connection. This has no effect if the connection is not established yet. Closing a socket will shut down the two I/O streams, and transition the socket's state from CONNECTED to either CLOSED or BOUND, if the local address has been explicitly specified and preserved.


public void connect(string remoteHost, int remotePort)

Connect this socket to a remote address/port.

If the socket has not bound yet, this will first bind the socket to the local address/port as specified in the constructors, should it have been instantiated in that fashion. Had no address/port been specified for this socket during construction, this method will first bind to a local address/port as provisioned by the OS, which comprises of a local address resolved from local host name (see Address.getLocal()), and an ephemeral local port.

Required Policies

  • System.Socket/connect

Parameters

  • remoteHost The remote host name to connect to.
  • remotePort The remote port to connect to.

public String getLocalHost()

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

Returns

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

public int getLocalPort()

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

Returns

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

public SocketStream getReadStream()

Get the input stream from which one can read data transmitted from the other side of the socket. The stream supports both synchronous and asynchronous reading, but is neither markable nor seekable.

Only one input stream instance will be associated with each socket. Calling this multiple times returns the same stream object. Closing this stream will cause the socket to be disconnected.

Access policy System.Socket/read must be enabled to perform read operation on this stream.

Returns

  • An input stream enabled for reading operations.

public String getRemoteHost()

Get the remote host for this socket. Only returns non-null value when the socket is in connection.

Returns

  • The remote host connected to this socket. Null if the connection is not established.

public int getRemotePort()

Get the remote port for this socket. Only returns non-zero value when the socket is in connection.

Returns

  • The remote port connected to this socket. 0 if the connection is not established.

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 socket.

Note that the socket's state always starts from UNBOUND, and can transition into either BOUND through bind() call. When it's connected, it will become CONNECTED, but a transition from UNBOUND directly into CONNECTED is also possible, if the local address has never been specifically designated before connect() call. When the connect is tore down, the state will go back to either CLOSED or BOUND, if the local address has been explicitly specified and preserved.

Socket in Julian is for one-shot use. A socket cannot be resurrected after disconnection.

Returns

  • The state of this socket.

public SocketStream getWriteStream()

Get the output stream to which one can send data out to the other side of the socket. The stream supports both synchronous and asynchronous reading, but is neither markable nor seekable.

Only one output stream instance will be associated with each socket. Calling this multiple times returns the same stream object. Closing this stream will cause the socket to be disconnected.

Access policy System.Socket/write must be enabled to perform read operation on this stream.

Returns

  • An output stream enabled for writing operations.