From: "Aleksi Niemelä" Date: 2000-09-20T04:05:16+09:00 Subject: [ruby-talk:5014] Re: Working server > A> 3) > A> For me the server for the above client does not work. > > It work for me Thanks Guy for such a prompt response. It made me update my version of Ruby, and brand new 1.6.0 seems to work well, just like you described. I even managed to modify tsrv.rb not to leave any closed connections (at least netstat won't display them as close anymore). The trick was to catch exceptions caused byt the socket access, and then ensure TCPSocket#close was called. Suddenly it became very clear that there was no "blocking" writes in the first place (so forget my mail which started this thread), but those raised exceptions, which just were not handled. Anyway here's a patch for tsvr.rb which won't leave closed connections behind, and is probably more robust anyway. - Aleksi Index: tsvr.rb =================================================================== RCS file: /home/cvs/ruby/sample/tsvr.rb,v retrieving revision 1.2 diff -u -r1.2 tsvr.rb --- tsvr.rb 2000/06/22 08:29:58 1.2 +++ tsvr.rb 2000/09/19 18:50:02 @@ -3,6 +3,8 @@ require "socket" +Thread.abort_on_exception=true + gs = TCPserver.open(0) addr = gs.addr addr.shift @@ -10,9 +12,17 @@ while TRUE Thread.start(gs.accept) do |s| - print(s, " is accepted\n") - while s.gets - s.write($_) + print("\n", s, " ") + print(s.peeraddr[2], " ", s.peeraddr[3], " at port ", s.peeraddr[1]) + print(" is accepted\n") + begin + while s.gets("\n") + print(s, " got #{$_.length}\n") + s.write($_) + print(s, " wrote back #{$_.length}\n") + end + rescue + print(s, " raised #$!\n") end print(s, " is gone\n") s.close It's output might look like this: # localhost 127.0.0.1 at port 1458 is accepted # got 4 # wrote back 4 # got 4 # wrote back 4 # got 4 # raised Broken pipe # is gone # localhost 127.0.0.1 at port 1459 is accepted # got 4 # wrote back 4 # got 4 # wrote back 4 # got 4 # raised Broken pipe # is gone