Provides network support
TCPSocket
TCP/IP socket
Operations
- New
- Close
- CloseGracefully
- Flush
- GetAddress
- GetLastError
- GetPort
- HostName
- IsOpen
- ReadBuffer
- ReadByte
- ReadLine
- Resolve
- Select
- SetConnectTimeout
- SetKeepAlive
- SetNoDelay
- SetRecvBufferSize
- SetRecvTimeout
- SetSendBufferSize
- SetSendTimeout
- WriteBuffer
- WriteByte
- WriteString
Close #
Closes the socket.
If this side has just written data and the peer has not read it yet, prefer `CloseGracefully()`. On Windows loopback, closing first can discard what was just written -- see that method and `docs/windows_loopback_sockets.md`.
method : public : Close() ~ NilCloseGracefully #
Closes the socket after letting the peer read what was written and hang up first.
On Windows over loopback, a peer that writes and then immediately closes can have the data it just sent discarded: the reader gets a connection reset and zero bytes rather than a short read, even though every byte was accepted and had arrived. The race is between this side's teardown and the peer's read, so the remedy is to not be the side that closes first. It is a platform behaviour, not something a socket option can turn off -- seven candidate fixes were measured and none worked (`docs/windows_loopback_sockets.md`).
This reads and discards anything still arriving until the peer closes, which is the point of the call, and then closes. Use it wherever a server writes a response and is finished with the connection:
Bounded on both sides, so it cannot park a thread: it gives up after `timeout_ms` of silence, and after 16 reads of a peer that keeps sending instead of closing. In the ordinary case -- the peer reads the response and hangs up -- it returns as soon as that happens, and costs nothing.
`Close()` stays correct when the peer has already closed, or when this side is the one being told to hang up. Linux and real network interfaces are unaffected either way; this matters on Windows loopback.
method : public : CloseGracefully(timeout_ms:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| timeout_ms | Int | how long to wait, in milliseconds, for the peer to close |
Example
socket->WriteString(response);
socket->CloseGracefully();Flush #
Flushes the output buffer. This is a no-opt given this socket is not backed by an output buffer.
method : public : Flush() ~ NilGetAddress #
Returns the connected network address
method : public : GetAddress() ~ StringReturn
| Type | Description |
|---|---|
| String | return network address |
GetLastError # function
Gets the calling thread's last socket failure: why its latest call on a socket -- connect, read, write, bind, listen, accept, a socket option -- failed, captured as it failed. Each such call replaces it, one that works leaves none, and closing the socket keeps it. A connect that tried several addresses gives the most informative reason: one the network answered (refused, reset, timed out) over one that never left this host. A name that does not resolve gives the resolver's reason.
function : GetLastError() ~ StringReturn
| Type | Description |
|---|---|
| String | last error message, or Nil if the calling thread's latest socket call worked |
GetPort #
Returns the connected network port
method : public : GetPort() ~ IntReturn
| Type | Description |
|---|---|
| Int | return network port |
HostName # function
Reads the host name
function : HostName() ~ System.StringReturn
| Type | Description |
|---|---|
| String | socket host name |
IsOpen #
Returns rather the socket is open
method : public : IsOpen() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if socket is open, false otherwise |
New # constructor
Connects to an address, trying each one the name resolves to (IPv6 and IPv4) until one connects. If none does, IsOpen() is false and GetLastError() says why.
New(address:System.String, port:Int)Parameters
| Name | Type | Description |
|---|---|---|
| address | String | network address |
| port | Int | network port |
New # constructor
Connects as New(address, port) does, giving each address tried at most connect_timeout milliseconds. If none connects, IsOpen() is false and GetLastError() says why ("timed out" when the time ran out).
New(address:System.String, port:Int, connect_timeout:Int)Parameters
| Name | Type | Description |
|---|---|---|
| address | String | network address |
| port | Int | network port |
| connect_timeout | Int | connect timeout in milliseconds (0 = blocking) |
ReadBuffer #
Reads bytes into a byte buffer
method : public : ReadBuffer(offset:Int, num:Int, buffer:Byte[]) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| offset | Int | destination buffer offset |
| num | Int | number of values to read |
| buffer | Byte | input buffer |
Return
| Type | Description |
|---|---|
| Int | number of values read |
ReadBuffer #
Reads bytes into a character buffer
method : public : ReadBuffer(offset:Int, num:Int, buffer:Char[]) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| offset | Int | destination buffer offset |
| num | Int | number of values to read |
| buffer | Char | input buffer |
Return
| Type | Description |
|---|---|
| Int | number of values read |
ReadLine #
Reads a string until a newline or character return is detected
method : public : ReadLine() ~ System.StringReturn
| Type | Description |
|---|---|
| String | character string |
Resolve # function
List of resolved IP addresses
function : Resolve(n:System.String) ~ System.String[]Parameters
| Name | Type | Description |
|---|---|---|
| n | String | address name |
Return
| Type | Description |
|---|---|
| String | list of IP addresses |
Select #
Checks if read/write data is available
method : public : Select(is_write:Bool) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| is_write | Bool | if true check for write, otherwise read |
Return
| Type | Description |
|---|---|
| Int | 1 if ready, 0 if not ready, -1 is error |
SetConnectTimeout #
Sets the connect timeout for the next connection attempt. Must be called before New() or before reconnecting.
method : public : SetConnectTimeout(ms:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| ms | Int | timeout in milliseconds (0 = blocking) |
SetKeepAlive #
Sets the TCP keep-alive option
method : public : SetKeepAlive(enable:Bool) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| enable | Bool | true to enable, false to disable |
Return
| Type | Description |
|---|---|
| Bool | true if successful |
SetNoDelay #
Disables Nagle's algorithm (TCP_NODELAY) for low-latency sends
method : public : SetNoDelay(enable:Bool) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| enable | Bool | true to disable Nagle, false to re-enable |
Return
| Type | Description |
|---|---|
| Bool | true if successful |
SetRecvBufferSize #
Sets the receive buffer size
method : public : SetRecvBufferSize(bytes:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| bytes | Int | buffer size in bytes |
Return
| Type | Description |
|---|---|
| Bool | true if successful |
SetRecvTimeout #
Sets the receive timeout
method : public : SetRecvTimeout(ms:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| ms | Int | timeout in milliseconds |
Return
| Type | Description |
|---|---|
| Bool | true if successful |
SetSendBufferSize #
Sets the send buffer size
method : public : SetSendBufferSize(bytes:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| bytes | Int | buffer size in bytes |
Return
| Type | Description |
|---|---|
| Bool | true if successful |
SetSendTimeout #
Sets the send timeout
method : public : SetSendTimeout(ms:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| ms | Int | timeout in milliseconds |
Return
| Type | Description |
|---|---|
| Bool | true if successful |
WriteBuffer #
Writes bytes from a byte buffer
method : public : WriteBuffer(buffer:Byte[]) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| buffer | Byte | input buffer |
Return
| Type | Description |
|---|---|
| Int | number of values written |
WriteBuffer #
Writes bytes from a byte buffer
method : public : WriteBuffer(offset:Int, num:Int, buffer:Byte[]) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| offset | Int | destination buffer offset |
| num | Int | number of values to write |
| buffer | Byte | input buffer |
Return
| Type | Description |
|---|---|
| Int | number of values written |
WriteBuffer #
Writes characters from a byte buffer
method : public : WriteBuffer(buffer:Char[]) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| buffer | Char | input buffer |
Return
| Type | Description |
|---|---|
| Int | number of values written |
WriteBuffer #
Writes characters from a character buffer
method : public : WriteBuffer(offset:Int, num:Int, buffer:Char[]) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| offset | Int | destination buffer offset |
| num | Int | number of values to write |
| buffer | Char | input buffer |
Return
| Type | Description |
|---|---|
| Int | number of values written |