# How to write a simple TCP server

**URL:** <https://racket.discourse.group/t/how-to-write-a-simple-tcp-server/3457>\
**Category:** Questions & Answers\
**Created:** [January 2, 2025, 1:38pm UTC](https://racket.discourse.group/t/how-to-write-a-simple-tcp-server/3457 "2025-01-02T13:38:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tinkererthinker](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/tinkererthinker/32/2108_2.png) [@tinkererthinker](https://racket.discourse.group/u/tinkererthinker)\
**Post date:** [January 2, 2025, 1:38pm UTC](https://racket.discourse.group/t/how-to-write-a-simple-tcp-server/3457/1 "2025-01-02T13:38:22Z")

</div>

I'm interested in network programming. In order to test my understanding of the TCP/IP protocol, I wanted to write a simple TCP client and server.

I read the [docs](https://docs.racket-lang.org/reference/tcp.html) but I don't know which procedures I would need to make it work.

Can anyone point me to some code samples I can study?

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [January 2, 2025, 2:09pm UTC](https://racket.discourse.group/t/how-to-write-a-simple-tcp-server/3457/2 "2025-01-02T14:09:45Z")

</div>

I would suggest you follow the tutorial [More: Systems Programming with Racket](https://docs.racket-lang.org/more/index.html), which walks through the implementation of a web server using the TCP procedures.

---

<div class="post-metadata">

**Author:** ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)\
**Post date:** [January 2, 2025, 2:09pm UTC](https://racket.discourse.group/t/how-to-write-a-simple-tcp-server/3457/3 "2025-01-02T14:09:57Z")

</div>

```scheme
#lang racket

(module common racket
  (provide send)

  (define (send x out)
    (write x out)
    (flush-output out)))

(module server racket
  (provide server)

  (require (submod ".." common))
  
  #; {PortNumber -> Void}
  ;; read 3 messages from clients, eprint them and sent them back
  (define (server port)
    (define listener (tcp-listen port 30 #true))
    (define-values (in-stream out-stream) (tcp-accept listener))
    (main in-stream out-stream)
    (close-input-port in-stream)
    (close-output-port out-stream))

  #; {InputPort OutputPort -> Void}
  ;; read 3 messages eprint them and sent them back 
  (define (main in-stream out-stream)
    {let while-loop ([i 3])
      (when (> i 0)
        (define the-client-sent (read in-stream))
        (eprintf "server says: the client sent ~a\n" the-client-sent)
        (send `[you sent ,the-client-sent] out-stream)
        (while-loop (- i 1)))}))

(module client racket
  (provide client)

  (require (submod ".." common))

  (define LOCALHOST "127.0.0.1")

  #; {PortNumber [optional-ip-address] -> Void}
  ;; send 'a 'b and 'c to the server's `port` (on at), receeive response, eprint
  (define (client port [at LOCALHOST])
    (define-values (in-stream out-stream) (tcp-connect at port))
    (main in-stream out-stream)
    (close-input-port in-stream)
    (close-output-port out-stream))

  #; {InputPort OutputPort -> Void}
  ;; send 3 messages eprint responses 
  (define (main in-stream out-stream)
    (for ([i '(a b c)])
      (send `[sending ,i] out-stream)
      (eprintf "the client says: ~a\n" (read in-stream)))))

(module+ test
  (require (submod ".." client))
  (require (submod ".." server))

  (thread (λ () (server 12345)))
  (thread (λ () (client 12345))))

```
