in reply to Re^4: Single user to multi user socket server
in thread Single user to multi user socket server
#!/usr/local/bin/perl -w 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 => 2323, Listen => 1, Reuse => 1) or die $!; my $sk = IO::Socket::INET->new('PeerAddr' => '192.168.3.100', 'PeerPor +t' =>'4004', 'Proto'=>'tcp') || die "Can't connect"; # 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(1) { if(@ready = $select->can_read(0)){ 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; my $lineB; $sk->recv($line, 1024); $socket->recv($lineB,1); $lineB=length($lineB); print "length ",length($lineB),"\n"; print "Ord ",ord($lineB),"\n"; print "Revc ",$lineB,"\n"; if($lineB > 0){ print "Hello\n"; $socket->send($line); } } } } }
|
|---|