tcp_server_stream


tcp_server_stream(address, @key port_number, backlog = 128,
                  reuse_address = true, transcoder)
      

Return a new tcp-server-stream from which network connections from clients are obtained.

Address indicates the internet address of the network interface on which connections requests are accepted, and possibly the IP port number. A string of the form "INTF:PORT" will be parsed and the value of INTF will be assigned to address and the integer value of PORT will be assigned to port_number. This argument can also be a 4 element byte-array (or u8array which contain the 32-bit IPv4 address or an 8 element byte-array which contain the 128-bit IPv6 address.

Port_number must be an exact integer. Indicates the IP port number assigned to the socket which accepts connection requests from clients.

Backlog must be a positive-exact-integer. Indicates the maximum number of connection requests that can be waiting to be accepted by a call to read (technically it is the value passed as the second argument of the UNIX listen() function). The default value of this setting is 128.

Reuse_address should be a boolean value. Controls whether it is possible to assign a port number that is currently active. Note that when a server process terminates, the socket it was using to accept connection requests does not become inactive immediately. Instead it remains active for a few minutes to ensure clean termination of the connections. A setting of false will cause an exception to be raised in that case. A setting of true will allow a port number to be used even if it is active. The default value of this setting is true.

Transcoder, if present, must be a transcoder object that will be used to encode and decode characters in the stream.

Examples:


// A simple network echo server:          
let s = tcp_server_stream(2020)

function echo(client)
{ showln(stream = client, read_line(client))
  flush_writer(client) }
  
letfn loop (client = read(s))
{ !call_with_stream(client, echo)
  loop(read(s)) }

// The following code can be used to test the echo server:
let c = tcp_client_stream("localhost:2020")
showln(stream = c, "hello")
flush_writer(c)
read_line(c)
//> hello          
      

Also see:

file_stream
pipe_stream
string_reader
string_writer
byte_array_reader
byte_array_writer
bits_reader
bits_writer


Core Module Index | Contents