Internet Socket Setup

OK guys… ive been asking a few networking questions lately… I have managed to get both my server and client sockets to communicate with each other on the same network. However, when I try to connect them from two different networks across the internet its a different story.

From what I heard, in order for it to work, do all I need to do is just change the address parameter from a local address to a global one? for ex: instead of 192.168.x.x, i just type in my Global ip address?

I tried this but still nothing works. both sockets are on port 2182. And this port seems to be working when they connect via the same network.

I think that the problem must be with my client socket. Because unlike my server socket, my client socket doesnt even show up in my “allow a program through windows firewall” list. My server shows up there, but my client never does. Here are both my server and client sockets:


#!/usr/bin/python # This is file

import socket # Import socket module

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 2182
s.bind((host, port))

while True:
c, addr = s.accept()
print(“Connection Obtained”)
DataOut = raw_input("Type Message: ")




import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = “Global IP Address of Serer”
port = 2182

s.connect((host, port))
while True:
InData = s.recv(1024)
if “honey booboo” not in InData:
note = open(“Special Message.rtf”, “w”)

I’m guessing that your server and client are connected to the Internet though a router or access point. Routers and APs do something called Network Address Translation (NAT) that effectively prevents devices from outside your network from directly contacting your computers at home unless you call out first.

You have to tell your router that you want to “forward” traffic that the router receives to a specific computer on your network. You will need to check the documentation on your router/AP for the specifics but the feature you are looking for is called “Port Forwarding”. You need to configure the router to forward port 2182 to your specific server machine.

For example, you have your router/ap on the internet and it has an IP address dynamically assigned by your ISP as (this is made up). Your ‘server’ probably has an address like (also made up). For somebody on the Internet to call your server, you have to configure your router to forward port 2182 to Then to reach your server, the client actually sends data to

The router will get the data on 2182 and then pass the data to your server at Everybody outside your network will see your server as

You will also have to configure your server to accept data on 2182 in your Windows Firewall settings.

Ok. I was thinking that just listening on a port was as good as port forwarding. So the problem then is with my server script correct? I dont need to do this with my client?

If the client and server can talk when they are on the same network then your code on both sides is fine. If the server is connected to a router/ap then the changes you have to do are:

  • Configure your router/AP to forward port 2182 to your server. You will need the documentation from your router/ap to know how to do this.
  • Change the client code to use the internet address from the router/AP. You can get this by logging into the router and checking the ‘status’ page.

thanks man. Ill let you know if it works after i get this port forwarded

it works!!! omg it works!!!

really thanks for helping me out…I started out programming bge. And bge is how I learned python. And now im using python for all sorts of things. And the beauty of it is the fact that I can use bge as part of my program. Make whatever I want…

Be careful though. Do religious checks on what you do with anything connected to the web. Python can suffer from sprays and null code exceptions and more since unicode is such a big deal with 3.x, just be sure to sanitize.

NP, network programming can be complicated and as you have found, if one thing is off then nothing works.

Im still using python 2.7. However, can you still elaborate on what you said about “cleaning” up?

Since you have an open port to the internet, it is public property now. In your program check all data coming in. Someone might discover the open port and throw random data at trying to fuzz it. Check if it’s the data you are looking for and throw the rest of it away.

I can do that by looking and “expecting” certain incoming data. What but how do I go about disposing the unwanted data?

Your best bet is to simple empty out the data that you did read and close the connection to the client.

When you do the recv method, just ignore what you don’t want, or clear the buffer. Also, setting the recv buffer size limit can help also.

oh. and how do you clear the buffer? Do I just do connection.recv() = “”?