Uses a hash table to record previously seen lines. You can run this program in DrRacket, but it makes more sense from the command line.
To run the example, install Racket, start DrRacket, paste the example
program into the top area in DrRacket, and click the Run
button. Alternatively, save the program to a file and run racket
on
the file.
Form and function names in the code are hyperlinked to documentation, so click on them for more information.
#lang racket ; An echo server (define listener (tcp-listen 12345)) (let echo-server () (define-values (in out) (tcp-accept listener)) (thread (lambda () (copy-port in out) (close-output-port out))) (echo-server))
Racket makes it easy to use TCP sockets and spawn threads to handle them. This program starts a server at TCP port 12345 that echos anything a client sends back to the client.
To run the example, install Racket, start DrRacket, paste the example
program into the top area in DrRacket, and click the Run
button. Alternatively, save the program to a file and run racket
on
the file.
Form and function names in the code are hyperlinked to documentation, so click on them for more information.
#lang racket ;; Report each unique line from stdin (let ([saw (make-hash)]) (for ([line (in-lines)]) (unless (hash-ref saw line #f) (displayln line)) (hash-set! saw line #t)))
#lang racket ; An echo server (define listener (tcp-listen 12345)) (let echo-server () (define-values (in out) (tcp-accept listener)) (thread (lambda () (copy-port in out) (close-output-port out))) (echo-server))
Draw more pictures or build a web server from scratch. Racket includes both batteries and a programming environment, so get started!