Create a network service that sits on TCP port 12321, which accepts connections on that port, and which echoes complete lines (using a carriage-return/line-feed sequence as line separator) back to clients. No error handling is required. For the purposes of testing, it is only necessary to support connections from localhost (127.0.0.1 or perhaps ::1). Logging of connection information to standard output is recommended.

The implementation must be able to handle simultaneous connections from multiple clients. A multi-threaded or multi-process solution may be used. Each connection must be able to echo more than a single line.

The implementation must not stop responding to other clients if one client sends a partial line or stops reading responses.

      INSTALL @lib$+"SOCKLIB"
      PROC_initsockets
 
      maxSess% = 8
      DIM sock%(maxSess%-1), rcvd$(maxSess%-1), Buffer% 255
 
      ON ERROR PRINT REPORT$ : PROC_exitsockets : END
      ON CLOSE PROC_exitsockets : QUIT
 
      crlf$ = CHR$13 + CHR$10
      port$ = "12321"
      host$ = FN_gethostname
      PRINT "Host name is " host$
 
      listen% = FN_tcplisten(host$, port$)
      PRINT "Listening on port ";port$
 
      REPEAT
        socket% = FN_check_connection(listen%)
        IF socket% THEN
          FOR i% = 0 TO maxSess%-1
            IF sock%(i%) = 0 THEN
              sock%(i%) = socket%
              rcvd$(i%) = ""
              PRINT "Connection on socket "; sock%(i%) " opened"
              EXIT FOR
            ENDIF
          NEXT i%
          listen% = FN_tcplisten(host$, port$)
        ENDIF
 
        FOR i% = 0 TO maxSess%-1
          IF sock%(i%) THEN
            res% = FN_readsocket(sock%(i%), Buffer%, 256)
            IF res% >= 0 THEN
              Buffer%?res% = 0
              rcvd$(i%) += $$Buffer%
              crlf% = INSTR(rcvd$(i%), crlf$)
              IF crlf% THEN
                echo$ = LEFT$(rcvd$(i%), crlf%-1)
                res% = FN_writelinesocket(sock%(i%), echo$)
                rcvd$(i%) = MID$(rcvd$(i%), crlf%+2)
              ENDIF
            ELSE
              PROC_closesocket(sock%(i%))
              PRINT "Connection on socket " ; sock%(i%) " closed"
              sock%(i%) = 0
            ENDIF
          ENDIF
        NEXT i%
 
        WAIT 0
      UNTIL FALSE
      END