rockmountain has asked for the wisdom of the Perl Monks concerning the following question:

I am posting my client and server program as it is. The program is failing miserably. Both of my programs are runinng in RH9. I am also struggling with extra characters in buffer for ex. in the statement like $buf = <"$message"> , it brings unwanted messages also cheers

Rock

#!/usr/bin/perl #IOServer.pl use warnings; use strict; use IO::Socket; use IO::Select; use Net::Ping; my $serverport = 23; # create a socket to listen on SOMAXCONN = 512 my $server = new IO::Socket( Domain => PF_INET, Proto => 'tcp', LocalPort => $serverport, Listen => SOMAXCONN, ); die "Cannot bind: $!\n" unless $server; # create a 'select' object and add server fh to it my $selector = new IO::Select($server); # stop closing connections from aborting the server $SIG{PIPE} = 'IGNORE'; # loop and handle connections print "Multiplex server started on port $serverport...\n"; while (my @clients = $selector->can_read) { $| = 1; # input has arrived, find out which handles(s) foreach my $client (@clients) { if ($client == $server) { # it's a new connection, accept it my $newclient = $server->accept; my $port = $newclient->peerport; syswrite $newclient, "You've reached the server port # + $port\n"; #my $port = $newclient->peerport; my $name = $newclient->peerhost; #print "New client $port:$name\n"; $selector->add($newclient); } else { # it's a message from an existing client my $port = $client->peerport; my $name = $client->peerhost; my ($message, $buf, $host); my $newclient = $client->accept; # my $port = $newclient->peerport; # my $reach = Net::Ping->new("icmp"); my $reach = Net::Ping->new($ > ? "tcp": "icmp"); (defined $reach) or die "Couldn't create Net::Ping object: $!\n +"; if (sysread $client, $message, 1024) { my $host = ""; my $host1 = ""; print "\n Client $name:$port sent: $message"; #while($newserver = $newserver->accept()) #{ while (defined ($buf = <"$message">)) { print "\n $buf"; $host = (split(/:/,$buf))[1]; $host1 = (split(/[\s\n]/,$host +))[0]; print "\nHost: '".$host1."'"; #print "Remote IP to be pinged + $host"; if($reach->ping("$host1")){ print "$host1 is reachable"; syswrite $client, "reachable +$host1 "; } else{ print "$host1 is unreachable +"; syswrite $client, "unreachab +le $host1"; } } # }#end of outer while {} syswrite $client, "\n Message received OK\n"; } else else { $selector->remove($client); $client->shutdown(SHUT_RDWR); print "\nClient disconnected\n"; # port, name +not defined } } } }#end of foreach loop
#!/usr/bin/perl use IO::Socket; $destination = 'www.yahoo.com'; $sock = new IO::Socket::INET (PeerAddr => 'xxx.xxx.xxx.xxx', PeerPort => 23, Proto => 'tcp', ); die "Socket could not be created. Reason: $!\n" unless $sock; #Enable autoflush $|=1; #foreach (1 .. 10) { # print $sock "Msg $_: How are you?\n"; # print $sock "ping:$destination \n"; #} print $sock "ping:$destination \n"; print "Client Connected. \n"; print "Server says: ",scalar(<$sock>); $sock->autoflush(1); print $sock "Message acknowledged from the client!\n"; print $sock "Good Bye! \n"; print "Ping result =>: ",scalar(<$sock>); #while(1) #{} close ($sock)

READMORE tags added by Arunbear

Replies are listed 'Best First'.
Re: Net::ping is failing. Any reason?
by liverpole (Monsignor) on Jul 23, 2006 at 13:49 UTC
    Hi rockmountain,

    I think you're going to need to supply more information.

    In the first place, please put <code> tags around the Client program to make it readable.  (It would also be helpful to label it as the Client program, the way you do with the Server program).

    Secondly, you should be mention what the PeerAddr should be changed to (I'm assuming the address of your local host, if Client and Server are both running locally).  Without that information, people may get frustrated when they get the error:

    Socket could not be created. Reason: Invalid argument

    Thirdly, there's a bug in your Server program.  You apparently have an extra else keyword which should be removed.

    Finally, please give clear, precise and simple instructions how to run the programs.  Don't assume that everyone will intuit that the Server program has to be run first, and the Client program second.  You should state the steps to follow, what output you expect to get, and what output you are getting, and specifically what it is that you think is incorrect.  When I run the Client, it seems to work fine for me:

    C:\Documents and Settings\liverpole\Desktop>ioclient.pl Client Connected. Server says: You've reached the server port # 1204 Ping result =>: reachable www.yahoo.com

    Is this the same as you're getting?  Is it different?   And in what way does it differ?

    If you can make it simple and easy to reproduce the problem, you will get better and quicker responses, with a higher probability of a useful solution.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/