1# A small example for a network server:
2
3LoadPackage("io");
4Print("Waiting for TCP/IP connections...\n");
5s := IO_socket(IO.PF_INET,IO.SOCK_STREAM,"tcp");
6IO_bind(s,IO_MakeIPAddressPort("127.0.0.1",8000));
7IO_listen(s,5);   # Allow a backlog of 5 connections
8
9terminate := false;
10repeat
11    # We accept connections from everywhere:
12    t := IO_accept(s,IO_MakeIPAddressPort("0.0.0.0",0));
13    Print("Got connection...\n");
14    f := IO_WrapFD(t,IO.DefaultBufSize,IO.DefaultBufSize);
15    repeat
16        line := IO_ReadLine(f);
17        if line <> "" and line <> fail then
18            Print("Got line: ",line);
19            IO_Write(f,line);
20            IO_Flush(f);
21            if line = "QUIT\n" then
22                terminate := true;
23            fi;
24        fi;
25    until line = "" or line = fail;
26    Print("Connection terminated.\n");
27    IO_Close(f);
28until terminate;
29IO_close(s);
30