Listening TCP acceptor wrapper for incoming connections.


Handle and result model


Methods

valid?

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

accept

(-- connection address result) Waits for one incoming connection and returns one connection wrapper, the remote IPv4 address as one host-order Nat32, and one result string. In DEBUG builds the acceptor must be accessed through a mutable Ref, for example @acceptor.accept; that form also works when DEBUG is FALSE.

Preconditions

  • The handle is valid.

Accept semantics


macOS backend


Examples

Accept one connection and print the remote address

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

{} Int32 {} [
  server: [
    result: String;
    acceptor: 0x7F000001n32 6612n16 listenTcp !result;
    [result.size 0 =] "listenTcp failed" ensure
    connection: address: @acceptor.accept !result;;
    [result.size 0 =] "accept failed" ensure
    address ipv4ToString print
    LF print
  ];

  client: [
    result: String;
    connection: 0x7F000001n32 6612n16 connectTcp !result;
    [result.size 0 =] "connectTcp failed" ensure
  ];

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

Expected Output

127.0.0.1

See also