reorganized some stuff, started experimenting with racket/gui

This commit is contained in:
Ibrahim Mkusa 2017-03-30 00:44:40 -04:00
parent bb0e49db30
commit 49a079922e
8 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,9 @@
#lang racket
(require racket/gui/base)
;; Create a new window via the frame class
(define frame (new frame% [label "Example"]))
;; Show frame(window) by calling it show method
(send frame show #t) ;; you call object methods via send

View File

@ -0,0 +1 @@
TCP communication racket concepts of events. For more see the racket guide.

View File

@ -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)))))