Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

binding server to already used port 80

by opensourcer (Monk)
on Jan 12, 2007 at 08:48 UTC ( #594327=perlquestion: print w/replies, xml ) Need Help??
opensourcer has asked for the wisdom of the Perl Monks concerning the following question:

hi, how can i bind to port 80(which is already used by apache). i want to bind to port 80 and listen for cli requests using the IO::Socket, while serving the web requests as well.

Replies are listed 'Best First'.
Re: binding server to already used port 80
by chargrill (Parson) on Jan 12, 2007 at 08:50 UTC

    You can't.

    Maybe you should write a CGI, or a mod_perl handler?

    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: binding server to already used port 80
by Corion (Pope) on Jan 12, 2007 at 08:51 UTC

    Only one program can bind and listen on one port at the same time. If you want to have "special" requests, you need to either configure Apache so it calls your script (via mod_perl or CGI, for exmaple), or you have to write your program so that it sits in front of Apache and forwards all "normal" requests to your program (via HTTP::Proxy for example).

    If all you want to do is to listen to the incoming requests, I wrote Sniffer::HTTP which listens to all network traffic and extracts the HTTP requests. So maybe you can tell us more about what you want to do.

      instead of having a mutiple requests( calling from 1. cli to server 2. calling from web(mod_perl) to server, i would like to have a single interface using port 80, so i can do the webwork as well as cli work using the port 80, the cli request would go to port 80 calling mod_perl and then to server.
Re: binding server to already used port 80
by f00li5h (Chaplain) on Jan 12, 2007 at 10:08 UTC

    You'll be able to get away with running many httpd's if you use a reverse proxy, or virtual hosts....

    But if the CLI you seek is a shell, via ssh or otherwise, you may be tough out of luck... as someone along the way may be transparently proxying your requests anyway...

    You may be able to run your sshd on port 443, the https port (since https is not allowed to be proxied/cached)

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
      Bind Server code
      #!/usr/bin/perl use IO::Socket; sub Start { $sock = new IO::Socket::INET (LocalAddr => 'xxxx', BindPort => 80, Proto => 'tcp', Listen => 10); die "$!" unless $sock; print "Starting Xtools\n"; my $new_sock = $sock->accept(); while(<$new_sock>) { print $_; } close($sock); } Start(); 1;
      The client Code
      #!/usr/bin/perl use IO::Socket; $document = "@ARGV"; print "\n"; $EOL = "\015\012"; $BLANK = $EOL x 2; my $host = "indrmlnx02"; my $sock = new IO::Socket::INET ( PeerAddr => $host, PeerPort => http(80), Proto => 'tcp', ); #or die "Could not create socket: +$!\n" unless $sock; #print $sock "Hello there!\n"; unless ($sock) { die "cannot connect to http daemon on $host" } $sock->autoflush(1); print "-------- \n"; print $sock "hello"; #print $sock "GET $document HTTP/1.1" . $BLANK; #while ( <$sock> ) { print } close $sock; #close($sock);
      when i try the above code i get no errors, but when i stop the server and run the client i get error, cannot connect to http daemon on xxxx at line xx.
      and i don't c the print's data, and no idea where it is going.

        You canít bind to a port more than once. Itís not possible, no matter how often you ask. Thereís no way to do it.

        Makeshifts last the longest.

Re: binding server to already used port 80
by tirwhan (Abbot) on Jan 12, 2007 at 15:29 UTC

    Not sure whether this is of any help in your particular situation, but you can bind to the same port on a single interface by giving said interface several IP addresses. This could be either several external IP addresses or several internal addresses in the range. So you could have your web server bind to port 80 on and have your RPC service bind to port 80 on (ditto for two external addresses). If your intention is to get around some firewall restrictions by using port 80 for telnet then this would work (albeit it may be a very bad idea).

    All dogma is stupid.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://594327]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2017-01-24 03:26 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (201 votes). Check out past polls.