Heh, I was writing some networking stuff yesterday too. Small world.
I'd make one change. Add (:use clojure.contrib.duck-streams) to your
namespace definition. Then you can change handle-client to this
(defn handle-client [_ client]
(spit (.getOutputStream client) message)
(doto client
.shutdownInput
.shutdownOutput
.close))
Take a look at duck streams, it will be really handy any time you do
something like this.
Hope that helps,
Sean
On Nov 13, 2:35 am, Ross Thomas <[email protected]> wrote:
> Hello group,
>
> First I'd like to thank Rich for such a great new tool. The assertion
> in Programming Clojure that it "feels like a general-purpose language
> beamed back from the near future" really sums things up very nicely.
> Homoiconicity + FP + STM + JVM = epic win.
>
> Anyway, thought I'd say hi, and solicit opinions on my first "non-
> trivial" (that's a relative term ;)) Clojure program, a toy server
> which opens a listening socket and prints a message to each connecting
> client:
>
> ;;;
>
> (ns seq-server
> #^{:author "Ross Thomas <[email protected]>"}
> (:import (java.net ServerSocket)))
>
> (def listen-port 8555)
> (def num-agents 32)
> (def message "Hello, world!\r\n")
>
> (def server-sock (ServerSocket. listen-port))
>
> (def clients (repeatedly #(.accept server-sock)))
>
> (def agent-pool (take num-agents (repeatedly #(agent nil))))
>
> (def agents (cycle agent-pool))
>
> (def work-seq (map vector clients agents))
>
> (declare handle-client)
>
> (defn run []
> (doseq [[c a] work-seq]
> (send-off a handle-client c)))
>
> (defn handle-client [_ client]
> (.. client getOutputStream (write (.getBytes message)))
> (doto client
> .shutdownInput
> .shutdownOutput
> .close))
>
> (run)
>
> ;;;
>
> I can't decide if doing it that way is a bit too weird or not, but it
> was fun to write. Anyway, interested to hear comments/criticisms, how
> I could make it more idiomatic, etc. :)
>
> -Ross
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en