HTTP server using perl HTTP::Daemon - only works from the same computer the server is running on

StackOverflow https://stackoverflow.com/questions/9842780

  •  26-05-2021
  •  | 
  •  

Question

I am going to create a simple HTTP server that I will also create some clients for it. Right now I am testing my simple server created in perl using HTTP::Daemon by a browser.

My server code is as follows:

use HTTP::Daemon;
use HTTP::Status;

my $d = HTTP::Daemon->new(LocalPort => 80,
                      Listen => 20) || die;

print "Web Server started!\n";
print "Server Address: ", $d->sockhost(), "\n";
print "Server Port: ", $d->sockport(), "\n";


print "Please contact me at: <URL:", $d->url, ">\n";
while (my $c = $d->accept) {

    print "received a request...\n";

    while (my $r = $c->get_request) {

        print "URI: " . $r->uri->path . "\n";

        if ($r->method eq 'GET' and $r->uri->path eq "/") {

            # remember, this is *not* recommended practice :-)

            $c->send_file_response("http_daemon_test.pl");

        }

        else {

            print "method: " . $r->method;

            print " uri:" . $r->uri . "\n";

            $c->send_error(RC_FORBIDDEN, "please do not try again")
        }
    }

    $c->close;

    undef($c);
}

Currently, my problem is that this server works correctly when I test it on the same machine. When I try to access the server by another computer in the same network, the server does not receive a request at all. Am I missing a mandatory (maybe simple? I'm a newbie!) step to expose the server to the outside world?

I appreciate your help with this regard,


Update: bellow is an example of the output I receive when I run the server:

Web Server started!

Server Address: 0.0.0.0

Server Port: 80

Please contact me at:

received a request...

received a request...

URI: /

received a request...

URI: /

received a request...

received a request...

URI: /


Latest update:

I realized that the problem is different from what I described above. After all, I could access my server from another computer. As you can see from the code, I'm not creating a separate thread to fulfill the request. Strangely, when I access my server from a browser, all requests from other computers will time-out until I close the browser I initially sent the request from (and got the answer for the request). Meanwhile, multiple requests from the same browser are fulfilled successfully. This does not happen, though, when I sent requests from clients written in Perl using libwww-perl library.

How does this translate to the initial problem? Every time I wanted to test my server, I did so first from the same machine using a browser. Then, I tried to access the server from another machine without closing the browser I initially tested the server with; and, as expected, it didn't work.

Thanks for your comments guys.

No correct solution

OTHER TIPS

Q: Does the PC (e.g. "myhttpserver") have a firewall? Have you tried disabling it?

Q: Have you tried "telnet myhttpserver 80"? Can you "ping httpserver"?

This sounds like it's more of a networking/network configuration question than a programming question. Please make sure the hosts can see each other, and that the http port is open.

It seems that you didn't define LocalAddr in parameters for HTTP::Daemon constructor. It possibly defaults to localhost, in which case you will see the symptoms exactly as you provided. Try to add

LocalAddr => '<your ip>'

to your constructor and see if it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top