* I think io-loop function can be improved/simplified.
* I usually don't like duplication (.readLine in) is being repeated in
io-loop.
* any other comments/links/reference to some other idiomatic code are
welcome.

(ns echo.server
  [:use clojure.contrib.server-socket clojure.java.io
clojure.contrib.logging])

(defn line-received [line out]
  (info line)
  (doto out (.write line) (.newLine))
  (.flush out))

(defn connection-closed [out]
  (info "Client connection closed."))

(def running (ref true))

(defn io-loop [in out]
  (loop [line (.readLine in)]
    (when @running
      (if (= line nil)
        (connection-closed out)
        (do
          (line-received line out)
          (recur (.readLine in)))
        ))))

(defn client-handler [in-stream out-stream]
  (try
    (io-loop (reader in-stream) (writer out-stream))
    (catch Throwable e (.printStackTrace e)))
  )
(info "starting server ...")
(create-server 8080 client-handler)
(info "server started")

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to