I am new to perl, so please excuse me for this stupid question.
I am trying to make a client program interact with a server program. This is working fine except for the fact that i can not make the server program send text to the client program. maybe it does, but the client program does not print it on the screen.
Can someone have a look at a snapshot of the code i used?
server.pl
######
Code:
while ($new_sock = $sock->accept()) {
$hostinfo = gethostbyaddr($new_sock->peeraddr);
$sel->add($new_sock); # let $sel manage the $new_sock handle
if ($sel->can_read()){
while (defined ($buf = <$new_sock>)) {
printf " Message from %s : $buf", $hostinfo->name || $new_sock->peerhost;
}
}
if ($sel->can_write()){
$msg="my text for the client here\n";
print $new_sock $msg;
}
}
client.pl
#####
Code:
$sock = IO::Socket::INET->new(PeerAddr => $host,
PeerPort => $port,
Proto =>'tcp');
die "Socket could not be created. Reason: $!\n" unless $sock;
$sel = IO::Select->new;
$sel->add($sock);
if ($sel->can_write()){
print $sock "hello server\n";;
}
if ($sel->can_read()){
# is it possible to read something ? TIME OUT =$time_out seconds,
while (defined ($buf = <$sock>)) {
# receive data
printf STDOUT $buf; # write them to STDOUT
}
} else {
print "Time out receiving data\n"; # print time out message
}
$sel->remove($sock);
close ($sock);