How to resolve the algorithm Echo server step by step in the Ol programming language
How to resolve the algorithm Echo server step by step in the Ol programming language
Table of Contents
Problem Statement
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Echo server step by step in the Ol programming language
Source code in the ol programming language
(define (timestamp) (syscall 201 "%c"))
(define (on-accept name fd)
(lambda ()
(print "# " (timestamp) "> we got new visitor: " name)
(let*((ss1 ms1 (clock)))
(let loop ((str #null) (stream (force (port->bytestream fd))))
(cond
((null? stream)
#false)
((function? stream)
(let ((message (list->string (reverse str))))
(print "# " (timestamp) "> client " name " wrote " message)
(print-to fd message))
(loop #null (force stream)))
(else
(loop (cons (car stream) str) (cdr stream)))))
(syscall 3 fd)
(let*((ss2 ms2 (clock)))
(print "# " (timestamp) "> visitor leave us. It takes " (+ (* (- ss2 ss1) 1000) (- ms2 ms1)) "ms.")))))
(define (run port)
(let ((socket (syscall 41)))
; bind
(let loop ((port port))
(if (not (syscall 49 socket port)) ; bind
(loop (+ port 2))
(print "Server binded to " port)))
; listen
(if (not (syscall 50 socket)) ; listen
(shutdown (print "Can't listen")))
; accept
(let loop ()
(if (syscall 23 socket) ; select
(let ((fd (syscall 43 socket))) ; accept
(fork (on-accept (syscall 51 fd) fd))))
(sleep 0)
(loop))))
(run 12321)
You may also check:How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Holidays related to Easter step by step in the Wren programming language
You may also check:How to resolve the algorithm K-d tree step by step in the Sidef programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Corescript programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Haxe programming language