Forum: Ruby Simple Server Question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Drew O. (Guest)
on 2007-02-06 16:44
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
Brian C. (Guest)
on 2007-02-06 17:06
(Received via mailing list)
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

Search for "Pop3Server"

HTH,

B.
Drew O. (Guest)
on 2007-02-06 17:24
> 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
Robert M. (Guest)
on 2007-02-10 09:40
(Received via mailing list)
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/.
>

Let go, and Let GOD!
Robert McCorkle
removed_email_address@domain.invalid
This topic is locked and can not be replied to.