TCP Server

Hi,

I am learning how to use Ruby to write a TCP Server. I used the sample code
in PickAxe, it worked fine, but I have no idea how I can let the program
connect more than one client? Do I need to use thread?

Can anyone show me how to do this by writing a echo server which can connect
multiple clients? And is there events like “on_connect” "on_disconnect"
available in ruby?

Thanks a lot.
Shannon

···

MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
http://join.msn.com/?page=features/virus

I am learning how to use Ruby to write a TCP Server. I used the sample code
in PickAxe, it worked fine, but I have no idea how I can let the program
connect more than one client? Do I need to use thread?

You have an example at

   http://www.ruby-lang.org/en/man-1.4/socket.html#TCPServer

Guy Decoux

“Shannon Fang” xrfang@hotmail.com wrote in message
news:F42SXaD8z9tTx7e4Vks000011ad@hotmail.com

Hi,

I am learning how to use Ruby to write a TCP Server. I used the sample
code
in PickAxe, it worked fine, but I have no idea how I can let the program
connect more than one client? Do I need to use thread?

Can anyone show me how to do this by writing a echo server which can
connect
multiple clients? And is there events like “on_connect” “on_disconnect”
available in ruby?

Take a look at

GServer.zip A generic, multi-threaded e-service server in Ruby. Includes a
simplistic HttpEchoServer.rb to demonstrate how to write your own server.

http://www.rogare.com/index.php?inc=downloads/ruby/ruby

Mikkel

Shannon,

Fork (Kernel.fork), thread(Thread), or poll (see below).

If you have linux (or other unices, even), you might want to check out Michael
Granger’s excellent Ruby-Poll library
(http://www.deveiate.org/code/Ruby-Poll.shtml) which includes a nice little
chatserver example (you also might want to check out MUES, but just b/c it
plainly rocks).

TCPServer (socket programming in general, actually) is a subject that comes up
pretty regularly; you should be able to find some additional examples via
Google.

~ Bruce

···

On Friday 13 December 2002 10:20 am, Shannon Fang wrote:

Hi,

I am learning how to use Ruby to write a TCP Server. I used the sample code
in PickAxe, it worked fine, but I have no idea how I can let the program
connect more than one client? Do I need to use thread?

Can anyone show me how to do this by writing a echo server which can
connect multiple clients? And is there events like “on_connect”
“on_disconnect” available in ruby?

Thanks a lot.
Shannon


MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
http://join.msn.com/?page=features/virus


Bruce R. Williams :: [iusris/#ruby-lang] :: http://www.codedbliss.com

‘It does not require a majority to prevail, but rather an irate,
tireless minority keen to set brush fires in people’s minds.’
– Samuel Adams

In case it's any use, here are a few snippets of fake POP3 server I wrote a
while back.

Here was my first attempt: the POP3 server instance is represented by its
own object.

···

On Sat, Dec 14, 2002 at 12:20:29AM +0900, Shannon Fang wrote:

I am learning how to use Ruby to write a TCP Server. I used the sample code
in PickAxe, it worked fine, but I have no idea how I can let the program
connect more than one client? Do I need to use thread?

Can anyone show me how to do this by writing a echo server which can
connect multiple clients?

-------------------------------------------------------------------------
#!/usr/local/bin/ruby -w

class Testserver
  def initialize(session)
    @session = session
  end
  def run
    @session.print "+OK I am a fake POP3 server\n"
    while line = @session.gets
      case line
        when /quit/i
          break
        else
          @session.print "-ERR I don't understand #{line}"
      end
    end
    @session.print "+OK bye\n"
    @session.close
  end
end

require 'socket'
port = (ARGV[0] || 110).to_i
server = TCPServer.new('0.0.0.0', port)
while (session = server.accept)
  Thread.new do
    Testserver.new(session).run
  end
end
-------------------------------------------------------------------------

The messy thing with that is having to keep referencing the instance
variable for the TCPSocket every time you want to input or output.

I tried subclassing TCPSocket, but I couldn't work out how to convert the
TCPSocket returned by server.accept into an instance of the subclass.

In the end, the tidiest solution seemed to be to dynamically add a 'run'
method to each TCPSocket as it is created:

-------------------------------------------------------------------------
#!/usr/local/bin/ruby -w

module Pop3
  def run
    print "+OK I am a fake POP3 server\n"
    while line = gets
      case line
        when /quit/i
          break
        else
          print "-ERR I don't understand #{line}"
      end
    end
    print "+OK bye\n"
    close
  end
end

require 'socket'
port = (ARGV[0] || 110).to_i
server = TCPServer.new('0.0.0.0', port)
while (session = server.accept)
  session.extend Pop3
  Thread.new do
    session.run
  end
end

-------------------------------------------------------------------------

It might not be quite as efficient though, although I don't know how much
overhead is associated with extending an object in this way.

Hope this gives you something to play with...

Regards,

Brian.

Hello Shannon,

Friday, December 13, 2002, 6:20:29 PM, you wrote:

Can anyone show me how to do this by writing a echo server which can connect
multiple clients? And is there events like “on_connect” “on_disconnect”
available in ruby?

from ruby distribution :slight_smile:

socket example - server side using thread

usage: ruby tsvr.rb

require “socket”

gs = TCPserver.open(0)
addr = gs.addr
addr.shift
printf(“server is on %s\n”, addr.join(“:”))

while TRUE
Thread.start(gs.accept) do |s|
print(s, " is accepted\n")
while s.gets
s.write($_)
end
print(s, " is gone\n")
s.close
end
end

···


Best regards,
Bulat mailto:bulatz@integ.ru