Skip to main content
GameDev.net gamedev.net
🔒 Locked

Python Sockets problem

Started by mikeman Jan 21, 2007 at 4:29 PM 11 replies 5.2k views
Original Post
mikeman
mikeman
Let's say I have the following server.py and client.py: server.py: mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) mySocket.bind ( ( "", 2323 ) ) mySocket.listen ( 1 ) channel,details=mySocket.accept() client.py: mysocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM) mysocket.connect( ('85.84.83.82',2323) ) '85.84.83.82' is my current dynamic IP. But whenever I execute those 2, I get in 1-2 seconds a 'Connection refused' error from client.py. Now I know that it finds the IP, because if I give another random one, it timesout. But why the connection is refused? If I substitute it with '127.0.0.1' it works fine. But as soon as I enter the actual IP, it doesn't. Any ideas on why this is happening? Am I missing something?
Zanthos
Zanthos
Assuming you are using an ADSL router, it may have a problem routing connections to it's WAN interface from inside the LAN(I know the Netgear DG834x has this problem, which is fixed in a patch).
mikeman
mikeman
I don't know. If I open the command prompt and ping my real IP, it doesn't seem to have a problem.

The weird thing is, if I connect my client to '85.84.83.82':80, then the connection is accepted. It seems to me that the problem is on the server side, the client can reach my real IP but it seems like noone listens to port 2323. I tried binding the real IP to the server too instead of '' but it doesn't accept it.
Todo
Todo
Are you sure you don't have a firewall running? Port 80 is probably configured automatically, but a random port above 1023 may not be. Connecting to the loopback address bypasses any firewall completely (AFAIK). Although not being able to bind to the external IP sounds a bit odd. I haven't programmed any sockets in a while and never in Python though.
GnuVince
GnuVince
Binding to the empty string will bind the socket to localhost. Enter the complete IP address instead.
smr
smr
Is your router forwarding connections on 80?
mikeman
mikeman
Ok, my latest try has been:

server.py:

host=socket.gethostname()
port=1234
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM)
mySocket.bind( (host, port) )

client.py:
host='xxx.xxx.xxx.xxx'//my real IP
port=1234
mysocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
p=mysocket.connect( (host,port) )

It still doesn't work. When I execute 'netstat -a -n', among others I get:

Protocol               Local IP              Foreign IP        StateTCP                    192.168.1.2:1234      0.0.0.0:0         LISTENING 


If I try to connect my client with '192.168.1.2' it works(and the 'Foreign IP' column is filled), but I wanted to do it with the real IP. But my server only listens to '192.168.1.2:1234', so if I try to connect the client to my real IP the connection is refused. And I can't bind the server to my real IP because it says 'can't assign requested address'. If I execute 'ipconfig', I get '192.168.1.2' as my IP. The 'real' IP I'm referring, which lools like '32.123.78.192', I only see it when I connect to '192.168.1.1' and see my router settings, and it's not always the same. Does all of this mess has to do with the fact that my IP is dynamic(it changes every time I disconnect/connect again)? Do I need to have a static IP in order to achieve what I'm trying here?

Thanks again for the help.
Zanthos
Zanthos
Quote:
Original post by GnuVince
Binding to the empty string will bind the socket to localhost. Enter the complete IP address instead.


That's incorrect. Binding to '' essentially binds to 0.0.0.0(all IP interface addresses assigned to the machine.. 127.x.x.x, and 192.168.1.2 from what I can tell).

Mikeman, you need to make sure...

1) Your router is forwarding port 80 (or whichever) to your machine on the LAN (192.168.1.2).

2) Your machine's firewall(if you have one) is allowing incoming connections to your app.

3) Your router allows connection attempts to 'jump out' of the LAN and back in through your Internet IP. Please remember some don't(as mentioned in my earlier, and very brief, post).

mikeman
mikeman
Ok, I'm not really an internet expert so bear with me. I have now disabled my firewall. As for port forwarding, currently there are no 'rules' applied. So I try to create one and this is exactly what I get:

WAN Connection: quick_start
LAN IP: 192.168.1.2

Rule name:example
Protocol:TCP
Port Start:????
Port End:????
Port Map:????

Assumming I want all this to work for port 2323, what values do I need to enter in "Port Start","Port End","Port Map"?

Also,there is a 'custom rule' option that has all the above fields and some additional ones(Application,Source IP,Destination IP,Source NetMask).
Vorpy
Vorpy
What type of router are you using? Most home routers just have the port start and port end boxes. Put 2323 for both the start and end to make it only forward that port. I don't know what Port Map does or if it is required. It probably allows you to forward connections from one port to another port, so just try leaving it blank for now.
Kylotan
Kylotan
Quote:
Original post by mikeman
If I try to connect my client with '192.168.1.2' it works(and the 'Foreign IP' column is filled), but I wanted to do it with the real IP. But my server only listens to '192.168.1.2:1234', so if I try to connect the client to my real IP the connection is refused. And I can't bind the server to my real IP because it says 'can't assign requested address'.


The fundamental problem here is that what you call your "real IP" address is not your IP address. That's why it doesn't show under netstat. It belongs to your router. It really has nothing to do with your computer. Requests to that address are just forwarded on your behalf if you've configured the router to do so. You are on a totally different network to the address you want to use; it just happens that your modem/router passes data across the boundary.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.