The posted code runs just fine. It passes the IP address to Server and receives the message, from command line . Problem is when I convert it to cgi, socket is not getting created, nor I get message that it is not getting created. I am pasting part of the cgi script I am trying to make. And yes I am beginner in CGI scripting.

print "<p class=\"systemMsg\">LINE AFTER SOCKET CREATION".$sock."</p>\n";

The web page shows till this line, but again $sock value is not getting printed. Hence I conclude that script has problem in creating socket. For more info, I am pasting my server script as well.

#!/usr/bin/perl #Client Script use IO::Socket; #domain name suffix - if we need it $domainName=""; $nodeName = ""; if (! $FORM{"\$selected_rows.Node"}) { print "<p class=\"systemMsg\">No Node Specified.</p></body></h +tml>"; die; } else { $nodeName = $FORM{"\$selected_rows.Node"}.$domainName; print "<p class=\"systemMsg\">Pinging host ".$nodeName."</p>\n +"; } $sock = new IO::Socket::INET (PeerAddr => '172.30.166.234', PeerPort => 23 , Proto => 'tcp', ); print "<p class=\"systemMsg\">LINE AFTER SOCKET CREATION".$sock."</p>\ +n"; die "Socket could not be created. Reason: $!\n" unless $sock; print "<p class=\"systemMsg\">Socket could not be created. Reason: $!\ +n".$sock."</p>\n"; print $sock "ping:".$nodeName."\n"; print "Client Connected. \n"; print "Server says: ",scalar(<$sock>); print $sock "Message acknowledged from the client!\n"; print $sock "Good Bye from client! \n";
#!/usr/bin/perl #Server Script 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) { # 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 at # $por +t\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($ > ? "tcp": "icmp"); (defined $reach) or die "Couldn't create Net::Ping object: $!\n"; if (sysread $client, $message, 1024) { print "\n Client $name:$port sent: $message\n"; #while($newserver = $newserver->accept()) #{ while (defined ($buf = <"$message">)) { print "Buffer: $buf"; $host = (split(/:/,$buf))[1]; print "Host: $host"; print "Remote IP to be pinged $host"; if($reach->ping("$host")){ # print "$host is reachable"; syswrite $client, "reachable +$host "; } else{ # print "$host is unreachable"; syswrite $client, "unreachab +le $host"; } } # }#end of outer while {} syswrite $client, "\n Message received OK\n"; } else { $selector->remove($client); $client->shutdown(SHUT_RDWR); print "\nClient disconnected\n"; # port, name not defined } } } }#end of foreach loop
cheers Rock

Edited by planetscape - added code tags; moved content out of signature (readability)

( keep:0 edit:4 reap:0 )


In reply to Re^2: Convering Perl socket program into CGI. by rockmountain
in thread Convering Perl socket program into CGI. by rockmountain

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.