SocketError when using DataInputStream with Clojure TCP server

125 views
Skip to first unread message

Dylan Gleason

unread,
May 23, 2014, 12:08:09 AM5/23/14
to clo...@googlegroups.com
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]
  (let [r (.readLine (clojure.java.io/reader 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.

Dylan Gleason

unread,
May 24, 2014, 2:27:23 PM5/24/14
to clo...@googlegroups.com
Silly error -- I neglected to bind DataInputStream to the socket's input stream, i.e. (let [reader (DataInputStream. (.getInputStream socket))] ... )


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/Jl5tED_hgIc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages