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
Type | Name | Signature |
---|---|---|
constructor | Socket | public Socket() |
constructor | Socket | public Socket(System.Network.SocketConfig) |
constructor | Socket | public Socket(string, int) |
constructor | Socket | public Socket(int) |
method | bind | public void bind(string, int) |
method | close | public void close() |
method | connect | public void connect(string, int) |
method | getLocalHost | public String getLocalHost() |
method | getLocalPort | public int getLocalPort() |
method | getReadStream | public SocketStream getReadStream() |
method | getRemoteHost | public String getRemoteHost() |
method | getRemotePort | public int getRemotePort() |
method | getSetting | public var getSetting(System.Network.TCPOption) |
method | getState | public SocketState getState() |
method | getWriteStream | public SocketStream getWriteStream() |
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
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
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
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
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
public String getLocalHost()
Get the local host for this socket. If binding has not happened yet, returns the value set in configuration.
Returns
public int getLocalPort()
Get the local port for this socket. If binding has not happened yet, returns the value set in configuration.
Returns
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
public String getRemoteHost()
Get the remote host for this socket. Only returns non-null value when the socket is in connection.
Returns
public int getRemotePort()
Get the remote port for this socket. Only returns non-zero value when the socket is in connection.
Returns
public var getSetting(TCPOption opt)
Get the setting for specified TCP option.
Parameters
Returns
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
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