I am working with the TCP server example from the
Clojure Cookbook to create an echo server. I am trying to modify it so that the receive function uses a DataInputStream and reads the incoming request as a byte array. However, I am having difficulty getting it to work. Here is what I have so far:
(defn receive
[socket]
(with-open [reader (DataInputStream. socket)]
(let [bytes-in (byte-array 146)]
(.read reader bytes-in)
(println "Made it here")))
(comment - This receive function works, the above doesn't
(defn receive
[socket]
(println r)))
)
(defn run
[port]
(let [running (atom true)]
(future
(with-open [socket (ServerSocket. port)]
(while @running
(with-open [sock (.accept socket)]
(receive sock)))))
running))
Executing the run function on the REPL:
> (tcp.server/run 8888)
Then, when I try to send a request via my TCP client, it results in a SocketException:
> (tcp.client/run :sync 1 10)
Transmitting message
-------------------------------
Message Size : 93 bytes
Host IP : 127.0.0.1
Host Port : 8888
Transmitting message
-------------------------------
Message Size : 93 bytes
Host IP : 127.0.0.1
Host Port : 8888
SocketException Broken pipe java.net.SocketOutputStream.socketWrite0 (SocketOutputStream.java:-2)
It appears that I am able to send two of the ten requests before a Broken pipe occurs, and on the server side it doesn't ever appear to make it to the receive function, as the println statement doesn't show any output. I know the client works, as I have tested it with my instructor's server, so it definitely appears to be a problem with the server.
If anyone has any insight, please share.