How to use TCP in Racket?

I tried to send a message from the client to the server and print the message on the server.

server.rkt:

#lang racket
(define the-listener (tcp-listen 9876))
(define-values (in out) (tcp-accept the-listener))
(displayln (read in))
(tcp-close the-listener)

client.rkt:

#lang racket
(define-values (in out) (tcp-connect "localhost" 9876))
(write "Hello" out)

I launched server.rktand then client.rktin the terminal. But the server prints #<eof>instead of a message Hello.

Why? And how to do it right?

+4
source share
1 answer

After sending the message, you need to clear the client-side output flush-output. Do not forget to carefully close the ports with close-input-portand close-output-portafter use, both on the client and on the server.

: , #<eof>, , , , end-of-file message ( " " ), eof-object?.

+5

Source: https://habr.com/ru/post/1525587/


All Articles