File: serverTCP.g

package info (click to toggle)
gap-io 4.7.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 772 kB
  • sloc: xml: 2,871; ansic: 2,685; makefile: 36; sh: 6
file content (29 lines) | stat: -rw-r--r-- 883 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# A small example for a network server:

LoadPackage("io");
Print("Waiting for TCP/IP connections...\n");
s := IO_socket(IO.PF_INET,IO.SOCK_STREAM,"tcp");
IO_bind(s,IO_MakeIPAddressPort("127.0.0.1",8000));
IO_listen(s,5);   # Allow a backlog of 5 connections

terminate := false;
repeat
    # We accept connections from everywhere:
    t := IO_accept(s,IO_MakeIPAddressPort("0.0.0.0",0));
    Print("Got connection...\n");
    f := IO_WrapFD(t,IO.DefaultBufSize,IO.DefaultBufSize);
    repeat
        line := IO_ReadLine(f);
        if line <> "" and line <> fail then
            Print("Got line: ",line);
            IO_Write(f,line);
            IO_Flush(f);
            if line = "QUIT\n" then
                terminate := true;
            fi;
        fi;
    until line = "" or line = fail;
    Print("Connection terminated.\n");
    IO_Close(f);
until terminate;
IO_close(s);