I’m working on a simple Chat Server/Client just to become familiar with
ruby socket programming. I’ve put together this very simple server that
accepts a connect and prints out the first message received. I’ve done
this in Java as well, but the issue I’m finding here is I cannot ctrl+c
to quit the server. Do I need to create a thread to listen for keyboard
input and then pass this to a quit method? It seems to me that ctrl+c
(or ctrl+z depending on os) should always force quite a running program,
right? In this case, it’s just hanging. Any help appreciated.
On Tue, Feb 06, 2007 at 11:45:35PM +0900, Drew O. wrote:
I’m working on a simple Chat Server/Client just to become familiar with
ruby socket programming. I’ve put together this very simple server that
accepts a connect and prints out the first message received. I’ve done
this in Java as well, but the issue I’m finding here is I cannot ctrl+c
to quit the server. Do I need to create a thread to listen for keyboard
input and then pass this to a quit method? It seems to me that ctrl+c
(or ctrl+z depending on os) should always force quite a running program,
right? In this case, it’s just hanging. Any help appreciated.
You didn’t say what platform you’re running on. It works for me, using
Linux
(Ubuntu 6.06):
$ ruby1.8 ert.rb 9999
Server running…
flurble << this sent from a client
ert.rb:16:in accept': Interrupt from ert.rb:16:inrun_server’
from ert.rb:27
If you are running Windoze, then I expect it’s blocking inside one of
the
socket calls.
I am running Windows (at work, mac at home) but I thought ruby’s thread
were “cross platform” as there were implemented purely in ruby. I even
added a thread to handle keyboard input in the on the server and it’s
still locking on my box. I see the output:
Server running…
Press any key to quit…
But nothing I press unlocks it. Am I doing something wrong here (revised
code below)?
require ‘socket’
class ChatServer
def initialize(port) @port = port @sessions = {}
end