Emeka, good catch. It's just my mistake.

Another thing is I think there may be exception raised when on-msg
sends message to a closed socket. How would you solve this?


On Oct 3, 7:35 pm, Emeka <emekami...@gmail.com> wrote:
> ngo,
> I was about doing this kind of client/server thing some  days ago, however
> now you are into it I would like to learn then. I am not quite clear why you
> have this:
> (.start (new Thread (fn [] (create-server 8080 chat-loop))))
>
> My concern is on  Thread, create-server function has a Thread inside
> create-server-aux function.
>
> Regards,
> Emeka
>
> On Fri, Oct 2, 2009 at 3:13 AM, ngocdaothanh <ngocdaoth...@gmail.com> wrote:
>
> > > I'm not sure TCP/IP has a native facility for that.
>
> > I'm afraid John's statement is correct:
>
> >http://www.velocityreviews.com/forums/t125620-client-socket-disconnec...
> > and trying to read and write until something wrong happens as
> > demonstrated in Roger's code is the only way to check for
> > disconnection.
>
> > Below is my new code. Sorry it is rather long.
>
> > (import '[java.io BufferedReader InputStreamReader
> > OutputStreamWriter])
> > (use 'clojure.contrib.server-socket)
>
> > (def clients (ref []))  ; Each client is an *out*
>
> > (defn on-msg [from msg]
> >  (println msg)
> >  (doall
> >    (map
> >      (fn [client]
> >        (if-not (= from client)
> >          (binding [*out* client]
> >            (println msg)
> >            (flush))))
> >     �...@clients)))
>
> > (defn on-disconnect [client]
> >  (dosync
> >    (alter clients
> >      (fn [clients]
> >        (remove (fn [c] (= c client)) clients))))
> >  (on-msg client "A client has disconnected"))
>
> > (defn on-connect [client]
> >  (dosync (alter clients conj client))
> >  (on-msg client "A client has connected"))
>
> > (defn chat-loop [is os]
> >  (let [client (OutputStreamWriter. os)]
> >    (on-connect client)
> >    (binding [*in* (BufferedReader. (InputStreamReader. is))]
> >      (loop []
> >        (let [msg (read-line)]  ; msg is nil when the client
> > disconnects
> >          (if (nil? msg)
> >            (on-disconnect client)
> >            (do
> >              (on-msg client msg)
> >              (recur))))))))
>
> > (.start (new Thread (fn [] (create-server 8080 chat-loop))))
>
> > On Oct 2, 5:20 am, John Harrop <jharrop...@gmail.com> wrote:
> > > On Thu, Oct 1, 2009 at 4:02 PM, Roger Gilliar <ro...@gilliar.de> wrote:
> > > > Am 01.10.2009 um 21:28 schrieb ngocdaothanh:
> > > > > Roger, your code is not event based.
> > > > What do you mean by not event based ?
>
> > > He means he wants automatic notification if a connection is dropped.
>
> > > I'm not sure TCP/IP has a native facility for that.
>
> > > What most chat type programs, multiplayer games, and suchlike do is send
> > a
> > > periodic ping from server to each connected client, which as part of the
> > > chat protocol the client is supposed to acknowledge. If a client stops
> > > responding for more than a few ping-intervals, it's assumed to have
> > > disconnected or otherwise become unreachable.
>
> > > This method has the advantage of being entirely under the control of the
> > > application layer, and the further advantage of also working with UDP
> > (which
> > > is crucial in the "multiplayer games" case at least).
--~--~---------~--~----~------------~-------~--~----~
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