Next: srfi basic-socket refs, Previous: srfi basic-socket spec, Up: srfi basic-socket [Index]
Simple echo server:
(import (vicare) (prefix (srfi :106 socket) srfi.)) (define (server-run master-socket) ;;Handle the first pending connection. If an ;;exception is raised ignore it. (guard (E (else (debug-print (condition-message E)))) (srfi.call-with-socket (srfi.socket-accept master-socket) (lambda (server-socket) (with-compensations (define in (compensate (transcoded-port (srfi.socket-input-port server-socket) (native-transcoder)) (with (close-port in)))) (define ou (compensate (transcoded-port (srfi.socket-output-port server-socket) (native-transcoder)) (with (close-port ou)))) (push-compensation (srfi.socket-shutdown server-socket) (srfi.socket-close server-socket)) (let loop ((line (read-line in))) (unless (eof-object? line) (put-string ou (string-append line "\r\n")) (flush-output-port ou) (loop (read-line in)))))))) ;;Handle next pending connection. (server-run master-socket)) (define echo-master-socket (srfi.make-server-socket "8080")) (server-run echo-master-socket)
Simple echo client:
(import (rnrs) (prefix (srfi :106 socket) srfi.)) (define client-socket (srfi.make-client-socket "localhost" "8080" (srfi.address-family inet) (srfi.socket-domain stream) (srfi.address-info v4mapped addrconfig) (srfi.ip-protocol ip))) (srfi.socket-send client-socket (string->utf8 "hello\r\n")) (display (utf8->string (srfi.socket-recv client-socket (string-length "hello\r\n")))) (flush-output-port (current-output-port)) (srfi.socket-shutdown client-socket (srfi.shutdown-method read write)) (srfi.socket-close client-socket)