I copied the following script and run it to have it listen on port 80. But netstat
doesn't show port 80. Why does netstat
not sow it, or the Perl script is not correct?
#!/usr/bin/perl -w
use Socket;
use IO::Handle;
$port=80;
$host='localhost';
$packhost=inet_aton($host);
$address=sockaddr_in($port,$packhost);
socket(SERVER,AF_INET,SOCK_STREAM,getprotobyname('tcp'));
bind(SERVER,$address);
listen(SERVER,10);
while( 1 ) {
next unless (accept(CLIENT,SERVER));
CLIENT->autoflush(1);
$msg_out="WHAT DO YOU WANT?\n";
send(CLIENT,$msg_out,0);
close CLIENT;
}
close SERVER;
exit 1;
What platform are you on? How are you invoking netstat
?
On Windows XP, after running the script with admin privileges, netstat -a gives me:
TCP aardvarkvi:http aardvarkvi:0 LISTENING
Binding to ports below 1024 requires root privileges on *nix
systems. Since you do not (or, shall I say, code you seem to have blindly copied does not) check the return values of various calls, you would not know if they failed.
In general, you should not have to use Socket.pm. Stick with IO::Socket and avoid blindly copying code without knowing what it does.
You might also want to look into HTTP::Daemon.