blob: 27a51516e9fe487e1f6e601450f0a1938bafbf22 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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")
|