aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-09 22:29:59 -0400
committerIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-09 22:29:59 -0400
commit404f3d7edfe648dc3fd51c1afc1b3984557f1902 (patch)
tree1bca1d7473572d16a11b58c78393bc63f453d6a6
parentf0d361b49c80d9fb78c67f006ad49b1062e8ed64 (diff)
Added tcpcommunication.rkt
-rw-r--r--tcpcommunication.rkt57
1 files changed, 57 insertions, 0 deletions
diff --git a/tcpcommunication.rkt b/tcpcommunication.rkt
new file mode 100644
index 0000000..27a5151
--- /dev/null
+++ b/tcpcommunication.rkt
@@ -0,0 +1,57 @@
+#lang racket
+(require math/base) ;; for random number generation
+
+(define listener (tcp-listen 4326 5 #t))
+(define a (thread (lambda ()
+ (define-values (s-in s-out) (tcp-accept listener))
+ ; Discard the request header (up to blank line):
+ ;(regexp-match #rx"(\r\n|^)\r\n" s-in)
+ (sleep 10)
+ (define (echo)
+ (define input (read-line s-in))
+ (displayln input s-out)
+ (flush-output s-out)
+ (if (eof-object? input)
+ (displayln "Done talking\n")
+ (echo)))
+ (echo)
+ (close-input-port s-in)
+ (close-output-port s-out)
+ (tcp-close listener)
+ 'ok)))
+
+(define t (thread (lambda ()
+ (define-values (c-in c-out) (tcp-connect "localhost" 4326))
+ (define input-prompt "input: ")
+ (define output-prompt "output: ")
+
+ ;; prompt for username and bind to a variable username
+ (display "What's your name?\n")
+ (define username (read-line))
+ (define usernamei (string-append username ": ")) ;; make username appear nicer in a prompt
+ (define fair (make-semaphore 1))
+
+ ;; intelligent read, quits when user types in "quit"
+ (define (read-loop-i)
+ ;(semaphore-wait fair)
+ ; (display usernamei)
+ (define input (read-line))
+ ;; do something over here with input maybe send it out
+
+ ;; Tests input if its a quit then kills all threads
+ ;; An if would be better here tbh
+ (cond ((string=? input "quit") (exit)))
+ (display (string-append output-prompt input "\n") c-out)
+ (flush-output c-out)
+ (displayln (read-line c-in)) ;; server echoes back sent input
+ ;(semaphore-post fair)
+ (read-loop-i)
+ )
+ (read-loop-i)
+ 'ok)))
+
+;(kill-thread a)
+;(kill-thread t)
+(thread-wait t)
+(display "DONE!!\n")
+