From 49a079922ebe30ea76ca00fc874cfa4017b1a510 Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Thu, 30 Mar 2017 00:44:40 -0400 Subject: reorganized some stuff, started experimenting with racket/gui --- feasibility_analysis/tcpevents/README.md | 1 + feasibility_analysis/tcpevents/server.rkt | 39 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 feasibility_analysis/tcpevents/README.md create mode 100644 feasibility_analysis/tcpevents/server.rkt (limited to 'feasibility_analysis/tcpevents') diff --git a/feasibility_analysis/tcpevents/README.md b/feasibility_analysis/tcpevents/README.md new file mode 100644 index 0000000..7c3ec21 --- /dev/null +++ b/feasibility_analysis/tcpevents/README.md @@ -0,0 +1 @@ +TCP communication racket concepts of events. For more see the racket guide. diff --git a/feasibility_analysis/tcpevents/server.rkt b/feasibility_analysis/tcpevents/server.rkt new file mode 100644 index 0000000..4313bda --- /dev/null +++ b/feasibility_analysis/tcpevents/server.rkt @@ -0,0 +1,39 @@ +#lang racket + +(define (serve in-port out-port) + (let loop [] + (define evt (sync/timeout 2 + (read-line-evt in-port 'any) + (thread-receive-evt))) + (cond + [(not evt) + (displayln "Timed out, exiting") + (tcp-abandon-port in-port) + (tcp-abandon-port out-port)] + [(string? evt) + (fprintf out-port "~a~n" evt) + (flush-output out-port) + (loop)] + [else + (printf "Received a message in mailbox: ~a~n" + (thread-receive)) + (loop)]))) + +(define port-num 4321) +(define (start-server) + (define listener (tcp-listen port-num)) + (thread + (lambda () + (define-values [in-port out-port] (tcp-accept listener)) + (serve in-port out-port)))) + +(start-server) + +(define client-thread + (thread + (lambda () + (define-values [in-port out-port] (tcp-connect "localhost" port-num)) + (display "first\nsecond\nthird\n" out-port) + (flush-output out-port) + ; copy-port will block until EOF is read from in-port + (copy-port in-port (current-output-port))))) -- cgit v1.2.3