开发者

Accessing my home network using CURL

This question sort of covers both ServerFault.com and here, but this seems more programming than server. I set up my home computer to run Apache server. It answers to port 5900, which has been port forwarded from my wireless router. I then set up a Dynamic DNS server to continually update what the IP address of my home network is. I know that part works, because I used a different computer on a different wireless network, and was able to access my server's index page using MYURL.com:5900.

My goal is to now send a message to 开发者_JAVA技巧my home server. I've written a script on my home server, where if I send it a POST message, it will save that message to a file. In other words, the series of events should go like this:

  1. I log on to my web page, write text in a Input, and hit a send button
  2. The message gets passed to my web site's server.
  3. My server runs a script that uses CURL to send the message as a POST to my home server's DDNS
  4. The server at home takes the post, and runs a script which writes it to a file.

I know how to do 1,2 and 4. I've been trying to get 3 to work and can't. I can't even get CURL to read my home server's index.html. Here's the code I've used with CURL (Using PHP):

    $string = 'http://MYURL.com';

    echo "sending to " . $string . '<br/>';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PORT, 5900);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $response = curl_exec($ch);

    if(curl_error($ch))
    {
        echo curl_error($ch);
    }
    echo $response;

    curl_close($ch);

I tested this with google.com, and the default port, and it worked fine. It echoed the html given to me by google. When I run it on my home server's DNS, however, it always times out. When I don't add a time out, it said it couldn't connect. This is using the exact same URL that, when put into a browser, correctly sends me my home server's index.html.

Does anyone know what's going wrong? Also, if there was a better way to do this, what is it?

I know this was a long question, so thank you so much.

Ethan


From where are you testing this? Most networks these days are heavily firewalled and disallow outgoing connections to strange ports. Your port 5900 certainly qualifies as strange.

The reason why your test using the wireless (I'm assuming 3G?) network works is because that PC/laptop happens to be connected directly to the internet and is therefore not blocked by a firewall.

The easiest way to test the firewall theory is to set up a test page on your server on port 80 and try accessing it using CURL. If that works then you know the problem is someone along the chain of routers and proxies is blocking your port 5900.


A little bit on why port 80 isn't considered "strange":

Port 80 is the port conventionally used for HTTP (web server) as assigned by IANA. As such it is usually not blocked considering that the primary reason people connect to the internet is to access the World Wide Web (which runs on HTTP and thus require port 80 to be open). Blocking port 80 makes the internet effectively useless (well, not quite, you still have email). If a sysadmin ever blocks port 80 he might as well disconnect the internet. Which is why by default port 80 is not blocked.

Now, what does this mean to you, the home-server-admin. Of course, your IP address only has one port 80. This means there can only really be one port-forwarding rule attached to port 80. This means that there can only be one computer on your local network serving web pages to the outside world*. If you little brother or sister wants to run another web server then he/she's going to have to use another port.

Which is why web servers are designed to server multiple web pages (they detect the URL so the single port 80 can server different sites). It's to allow a single IP address to server different web sites from a single machine. Google: http virtual host for more info.

*note: Not quite true, you can use a load balancing or re-routing proxy to redirect the HTTP request to other machines on the network. But the principle is still true. Only one machine's port 80 can be directly exposed to the internet. The others are just proxied.


Sometime the router prevent you to access your external IP adress from the internal network.

Try to telnet your own external IP and access the 5900 port from your internal network, you'll see if it's the case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜