0

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;
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Daniel
  • 621
  • 3
  • 9
  • 18

3 Answers3

3

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.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • Your link to HTTP::Daemon is broken (you only put 1 ':', not 2). The correct URL is: http://search.cpan.org/perldoc?HTTP::Daemon – Weegee Jul 12 '09 at 17:35
  • http://www.w3.org/TR/html401/struct/text.html#edef-KBD Whoa, are you actually using for "Indicates text to be entered by the user", or just because of its visual effect? :) – ephemient Jul 13 '09 at 21:19
  • Actually, I used it to 'indicates text to be entered by the user'. The fact that SO styles prominently is a mixed blessing. I would actually find it more pleasing with less of a bevel effect (or whatever it is called ;-) – Sinan Ünür Jul 13 '09 at 21:26
0

It's likely that netstat is replacing the numeric port number by the name from /etc/services. For example:

    ~, 503> netstat -a | more
    Active Internet connections (servers and established)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State
    tcp        0      0 *:svn                   *:*                     LISTEN

One thing that you can do is grep netstat's output to find all sockets where it's listening:

    netstat -a | grep LISTEN | grep tcp

You can also tell netstat to show numeric addresses rather than doing a hostname or services lookup (and there's another option where you can limit just port numbers; do man netstat):

    netstat -an | grep LISTEN | grep tcp
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
kdgregory
  • 38,754
  • 10
  • 77
  • 102
0

Sorry, my fault, when I run netstat, I didn't put the option -a. When use netstat -a, it shows that port.

Daniel
  • 621
  • 3
  • 9
  • 18
  • By default, `netstat` only lists connected sockets. `-a` lists all sockets, and Linux's `netstat` also understands `-l` to list listening sockets. – ephemient Jul 13 '09 at 21:17