in reply to Re: Can a socket listen for multiple hosts?
in thread Can a socket listen for multiple hosts?
Thanks for all the help! With multiple servers sending at the same time, it keeps track (using peerhost) which host sends the message, and of course the message contents.#!/usr/bin/perl use IO::Socket; use IO::Select; my $socket =new IO::Socket::INET->new ( LocalHost => "<ipaddress>", LocalPort => '<port>', Proto => 'tcp', Listen => 1, Reuse => 1, ); die("Couldn't create socket! $!\n") unless $socket; my $select = new IO::Select($socket); while(@ready = $select->can_read) { foreach $fh (@ready) { if($fh == $socket) { $new = $socket->accept; $select->add($new); my $host= $new->peerhost; print "[Accepting connection from $host]\n"; } else { my $line = <$fh>; $line =~ s/\s+$//; if($line =~/^quit$/i) { my $host = $fh->peerhost; $select->remove($fh); $fh->close; print "[Connection from $host terminat +ed\n"; } else { print $fh->peerhost, " said '$line'\n" +; print $fh "You said: '$line'\n"; } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Can a socket listen for multiple hosts?
by ikegami (Patriarch) on Jan 22, 2010 at 21:25 UTC | |
by sierpinski (Chaplain) on Jan 27, 2010 at 17:57 UTC | |
by ikegami (Patriarch) on Jan 27, 2010 at 19:06 UTC | |
by sierpinski (Chaplain) on Feb 01, 2010 at 13:36 UTC | |
by ikegami (Patriarch) on Feb 01, 2010 at 15:13 UTC | |
by sierpinski (Chaplain) on Aug 16, 2010 at 15:16 UTC |