Skip to content

Possible Bug in 05_network-io/5-10_tcp-server.asciidoc code. #484

@KevinSerafini

Description

@KevinSerafini

If I load the code for serve-persistent as written, my telnet session closes immediately after it receives the response. It appears that the problem is cased by having the second with-open inside the while loop. I changed the code a bit and it appears to work as expected:

(defn serve-persistent [port handler]
  (let [running (atom true)]
    (future
      (with-open [server-sock (ServerSocket. port)
                  sock (.accept server-sock)]
         (while @running
            (let [msg-in (receive sock)
                  msg-out (handler msg-in)]
              (send sock msg-out)))))
      running))

Also note that send conflicts with clojure.core/send, so in my code, I've renamed it to send-msg. I didn't change it here to match what is above.

Thanks,
Kevin

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions