Simple Server Question

D

Drew Olson

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
 
B

Brian Candler

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

Search for "Pop3Server"

HTH,

B.
 
D

Drew Olson

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top