Connected TCP stream wrapper.


Handle and result model


Methods

valid?

(-- valid) Reports whether the handle refers to one connection.

read

(buffer -- size result) Reads into one Nat8 Span and returns the transferred byte count plus one result string.

Preconditions

  • The handle is valid.
  • buffer is one Nat8 Span.

readString

(size -- string result) Reads at most size bytes and returns them as one String plus one result string.

Preconditions

  • The handle is valid.

write

(source -- result) Writes one source convertible to Nat8 Cref Span and returns one result string.

Preconditions

  • The handle is valid.
  • source is convertible to Nat8 Cref Span.

shutdown

(-- result) Closes the sending side of the connection and returns one result string.

Preconditions

  • The handle is valid.

Data transfer semantics


macOS backend


Examples

Example: send bytes and perform one stream read

The client sends hello and prints the server's reply from one read, which may be only a prefix of reply because TCP does not preserve write boundaries; repeated reads collect all five bytes.

"String"    use
"control"   use
"sync/sync" use

{} Int32 {} [
  server: [
    result: String;
    acceptor: 0x7F000001n32 6613n16 listenTcp !result;
    [result.size 0 =] "listenTcp failed" ensure
    connection: address: @acceptor.accept !result;;
    [result.size 0 =] "accept failed" ensure
    message: 32 @connection.readString !result;
    [result.size 0 =] "readString failed" ensure
    "reply" @connection.write !result
    [result.size 0 =] "write failed" ensure
  ];

  client: [
    result: String;
    connection: 0x7F000001n32 6613n16 connectTcp !result;
    [result.size 0 =] "connectTcp failed" ensure
    "hello" @connection.write !result
    [result.size 0 =] "write failed" ensure
    message: 32 @connection.readString !result;
    [result.size 0 =] "readString failed" ensure
    message print
    LF print
  ];

  serverContext: @server () spawn;
  clientContext: @client () spawn;
  @serverContext.wait
  @clientContext.wait
  0
] "main" exportFunction

Expected Output

reply

See also