Hello, all!
I have joined to this mailing list just now, and I am
newbie in Ruby. So, sorry, if my question seemed as very
easy, but I can not solve it. Please help me, if possible.
Before Ruby I have programmed on Perl, but in some reasons
I was have to go to the Ruby. And I have to write a socket
server on Ruby. Because I have some examples on Perl, I
decided to translate it to Ruby for meeting, how it can be
done on Ruby.
So, I have a simply echo socket server on Perl:
···
#!/usr/bin/perl -wT
require 5.002;
use strict;
use IO::Socket;
use IO::Select;
create a socket to listen to a port
my $listen = IO::Socket::INET->new(Proto => ‘tcp’,
LocalPort => 2324,
Listen => 1,
Reuse => 1) or die $!;
to start with, $select contains only the socket we’re
listening on
my $select = IO::Select->new($listen);
my @ready;
wait until there’s something to do
while(@ready = $select->can_read) {
my $socket;
# handle each socket that's ready
for $socket (@ready) {
# if the listening socket is ready, accept a new
connection
if($socket == $listen) {
my $new = $listen->accept;
$select->add($new);
print $new->fileno . “: connected\n”;
} else {
# otherwise, read a line of text and send it back
again
my $line="";
$socket->recv($line,80);
$line ne “” and $socket->send($line) or do {
# either can't send or can't receive, so they must have
hung up
print $socket->fileno . “: disconnected\n”;
$select->remove($socket);
$socket->close;
};
}
}
}
I tryed to translate it into Ruby, but I did not find in
Ruby any objects as Perl IO::Select, so
how I can handle multiple clients on Ruby as Perl do it
with IO::Select?
May be there are some any additional documentation about
writing socket servers on Ruby?
Please, help me.
Best regards,
Alexandr Vladykin
moscower@smtp.ru