tags:

views:

50

answers:

2

Hi everyone, I am trying to make a simple server/client program pair. On LAN they work fine, but when i try to connect from the "outside" it says connection refused. I shut down firewalls on both machines but i am still unable to connect, and i double checked the ip.

What am i doing wrong?

Thanks

Jake

Code:

import socket 
host = '' 
port = 9888 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.bind((host,port))     
s.listen(1) 
conn, adrr = s.accept() 
conn.send("Hello, world!") 
s.close() 

Client: 
import socket 
host = '68.x.x.x'
port = 9888
s = socket.socket(socket.AF_INET, socket_SOCK_STREAM)
s.connect((host,port)) 
print s.recv(200) 
s.close() 
A: 

How's the argument you're passing to the .bind call for your server socket? That's the single likeliest cause -- e.g. if you're using 192.168.x.y for whatever values of x and y, or 10.x.y.z likewise, that's a local-network address only, not routed by inter-network routers by internet conventions (most routers can be programmed to forward some incoming packets to a specific local-network address, typically depending on ports, but that's very specific to router's brands and models).

Alex Martelli
So basically i have to forward ports?
Jake
@Jake, unless your computer is running with an externally-accessible static IP on a public subnetwork (i.e. **not** 192.168.etc, or 10.etc), yes, port-forwarding from your router (who had _better_ have an externally accessible public IP -- if not static, you can use dyndns or similar services to ameliorate that) is the way to go. See superuser.com for more details, as "how do I best obtain port forwarding on this specific model of router" is not a programming question per se.
Alex Martelli
@Jake: Yes. It's extremely likely that you are locally using "RFC1918" addresses (that's the IETF standards proposal document which discussions the sets of reserved addresses that are used for isolated networks and which, over the years, have been used when doing NAT (network address translation) on the borders between small and home LANs and the Internet.It's possible that your ISP may block almost arbitrary lists of ports and types of traffic through their routing infrastructure for various reasons. This is less common with better ISPs and more common with cable companies, for example.
Jim Dennis
+2  A: 

You have one of two possible issues.

  1. Erroneous network configuration
  2. Bug(s) in code

The way to debug this is to try and rule one out. If we can get rid of the Code issue then we know it is a network issue.

Get a Socket Server and client that you know works and then try them as standalone programs. inside and outside of the firewall. Go to this site and download the examples. Change the ports in both the client and the server, compile and run them. First on same machine within network, second from two machines on same network and then server from within and client from outside of network.

Romain Hippeau