base.Channel.openServerSocket

Open a TCP server socket channel.

Syntax

openServerSocket(
   interface STRING,
   port INTEGER,
   mode STRING )
  1. interface is the name of the network interface to be used.
  2. port is the port number of the service.
  3. mode is the open mode. Only "u" is allowed (combined with "b" if needed).

Usage

The openServerSocket() method initializes the channel object to listen to a given TCP interface and port.

The server socket accepts multiple client connects: After calling the openServerSocket() method, a call to readLine() waits until the first client connects and returns after reading a complete line. Only one client connection can be serviced at time: it's not possible to select a specific client connection. A client connection must be closed by writing the EOF character to the channel. The EOF character is ASCII 26. Do not call base.Channel.close() to close a client/server connection: This would close the sever socket and reject any pending client connection. The next call to readLine() after writing EOF will wait until the next client connects or select the next pending client.

Pay attention to character set used by the network protocol you want to use by opening a channel with this method: The protocol must be based on ASCII, or must use the same character set as the application.

The interface parameter defines the network interface to be used, in case if the server uses different network adapters. Use NULL to listen to all network interfaces, or when the server has only one network interface.

The port parameter defines the TCP port to listen to.

The opening mode must be "u", to read and write from/to the socket. The method will raise error -8085 if the mode is different from "u".

The "u" mode can be combined with the "b" binary mode, to avoid CR/LF translation on Windows platforms.

Note: The binary mode is only required in specific cases, and will only take effect when writing data.

The method raises error -8084 if the socket cannot be opened.

Example

MAIN
    DEFINE io base.Channel
    DEFINE s STRING
    LET io = base.Channel.create()
    CALL io.openServerSocket("127.0.0.1", 4711, "u")
    WHILE TRUE
        LET s = io.readLine() 
        CALL io.writeLine(s)
        -- next line closes the current connection
        CALL io.writeLine(ASCII 26) -- EOF
    END WHILE
END MAIN