Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -1 +1,3 @@
Write a server for a minimal text based chat. People should be able to connect via telnet, sign on with a nickname, and type messages which will then be seen by all other connected users. Arrivals and departures of chat members should generate appropriate notification messages.
Nov. 31st is an interesting deadline :-) --[[User:Walterpachl|Walterpachl]] ([[User talk:Walterpachl|talk]]) 10:46, 2 November 2013 (UTC)

View file

@ -1,21 +1,37 @@
#lang racket
(define outs (list (current-output-port)))
(define ((tell-all who o) line)
(for ([c outs] #:unless (eq? o c)) (displayln (~a who ": " line) c)))
(define *client-outputs* (list (current-output-port)))
(define ((tell-all who current-out) line)
(for ([out *client-outputs*] #:unless (eq? current-out out))
(displayln (~a who ": " line) out)))
(define (serve-client input output)
(define nick (begin
(display "Nick: " output)
(read-line input)))
(define tell (tell-all nick output))
(define ((client i o))
(define nick (begin (display "Nick: " o) (read-line i)))
(define tell (tell-all nick o))
(let loop ([line "(joined)"])
(if (eof-object? line)
(begin (tell "(left)") (set! outs (remq o outs)) (close-output-port o))
(begin (tell line) (loop (read-line i))))))
(cond [(eof-object? line)
(tell "(left)")
(set! *client-outputs* (remq output *client-outputs*))
(close-output-port output)]
[else
(tell line)
(loop (read-line input))])))
(define (chat-server listener)
(define-values [i o] (tcp-accept listener))
(for ([p (list i o)]) (file-stream-buffer-mode p 'none))
(thread (client i o)) (set! outs (cons o outs)) (chat-server listener))
(define-values [client-input client-output] (tcp-accept listener))
(file-stream-buffer-mode client-input 'none)
(file-stream-buffer-mode client-output 'none)
(thread (λ ()
(serve-client client-input client-output)))
(set! *client-outputs* (cons client-output *client-outputs*))
(chat-server listener))
(void (thread (λ() (chat-server (tcp-listen 12321)))))
((client (current-input-port) (current-output-port)))
(define (start-server)
(chat-server (tcp-listen 12321)))
(void (thread start-server))
(client (current-input-port) (current-output-port))