# Simple Server Question

**URL:** <https://www.ruby-forum.com/t/simple-server-question/84621>\
**Category:** Ruby\
**Created:** [February 6, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/simple-server-question/84621 "2007-02-06T15:44:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [February 6, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/simple-server-question/84621/1 "2007-02-06T15:44:51Z")

</div>

Guys -

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.

-Dre2

require ‘socket’

class ChatServer

def initialize(port)  
@port = port  
end

def run\_server

```
@sessions = {}
@my_server = TCPServer.new('localhost',@port)

puts "Server running...."

while(session = @my_server.accept)
  @action = session.gets
  puts @action
end

@my_server.close

```

end

end

my\_server = ChatServer.new((ARGV[0] || 80).to\_i)  
my\_server.run\_server

---

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [February 6, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/simple-server-question/84621/2 "2007-02-06T16:06:29Z")

</div>

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:in `run\_server’  
from ert.rb:27

If you are running Windoze, then I expect it’s blocking inside one of  
the  
socket calls.

BTW there is a fuller server example, which handles each incoming  
connection  
in its own thread, at  
[http://wiki.rubygarden.org/Ruby/page/show/SingletonTutorial](http://wiki.rubygarden.org/Ruby/page/show/SingletonTutorial)

Search for “Pop3Server”

HTH,

B.

---

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [February 6, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/simple-server-question/84621/3 "2007-02-06T16:24:11Z")

</div>

> If you are running Windoze, then I expect it’s blocking inside one of  
> the  
> socket calls.
> 
> BTW there is a fuller server example, which handles each incoming  
> connection  
> in its own thread, at  
> [http://wiki.rubygarden.org/Ruby/page/show/SingletonTutorial](http://wiki.rubygarden.org/Ruby/page/show/SingletonTutorial)
> 
> Search for “Pop3Server”
> 
> HTH,
> 
> B.

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

def run\_server

```
@my_server = TCPServer.new('localhost',@port)

Thread.new(self){|server| InputHandler.new(server).run}

while(session = @my_server.accept)
  puts session.gets
  session.close
end

```

end

def quit  
@my\_server.close  
exit  
end

end

class InputHandler  
def initialize(server)  
@server = server  
puts “Server running…”  
end

def run  
puts “Press any key to quit…”  
gets  
server.quit  
end  
end

my\_server = ChatServer.new((ARGV[0] || 80).to\_i)  
my\_server.run\_server

---

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [February 10, 2007, 8:40am UTC](https://www.ruby-forum.com/t/simple-server-question/84621/4 "2007-02-10T08:40:45Z")

</div>

any luck with this? I’m lookin to use a chat server in my app but  
haven’t had the guts to look into it yet…

On Tuesday February 6,2007, at February 6, 8:45AM, Drew O. wrote:

> keyboard
> 
> my\_server = ChatServer.new((ARGV[0] || 80).to\_i)  
> my\_server.run\_server
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Let go, and Let GOD!  
Robert McCorkle  
removed\_email\_address@domain.invalid
