Problems sending data over internet using socket module

i have set up a simple… YES SIMPLE its actualy less that 30 lines total … networking system that works localy. it has no designated server or client, the idea is that the two computers just exchange information. im having trouble geting the message to other computers though. im not sure what is wrong.

ive replaced my actual ip address with a fake one for security. but i am sure i have the right ones typed in.

from bge import logic
import pickle
import socket
class Recever():
    def __init__(self,host = "", port = 9999):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    def receve(self):
        while True:
                data, addr = self.socket.recvfrom(1024)
                raw_data = pickle.loads(data)
                return raw_data
            except socket.error:
                return None
Recever = Recever()
def receve():
    Item = Recever.receve()
    if Item != None:
        logic.getCurrentController().owner.worldPosition = Item

from bge import logic
import pickle
import socket
def send(Item):
    sock = socket.socket(socket.AF_INET , socket.SOCK_DGRAM)
    data = pickle.dumps(Item) #ip address goes in ""
    sock.sendto(data,("",9999)) #ip address goes in ""
def main():

Try binding the client to (’’, port) instead of directly entering the remote ip. You may also need to port forward.

Sent from my Nexus 7 using Tapatalk HD

Thanks agoose works like a charm.

now. a logistics question. how would two computers wanting to connect exchange ip addresses?

An intermediary matchmaker. Hence “Matchmaking servers”. You can escape port forwarding for the most part using this form of “punchthrough” -

For testing purposes you can register and download some NoIP software or DynDNS so you have something similar to a static IP while hosting a testing server.

correct me if I am wrong but if you have 10, 100, 1000 connections all going through port 9999, they are still going to all use different socket/port numbers, through port 9999 correct?

you can bind only one socket to a port

so for 10 connections I would have to have 10 different Ports? that doesn’t seem right.

Well if you want 10 separate connections for some reason then yes.
But for a simple client-server network you wont need more then 1 or 2.

The client needs only one socket and can use it to send to any number of different servers. The same is with the server, you bind only one socket and can use it for any number of clients. You can use 2 sockets on the server if you want to separate the lobby from the game.

The port is singular when thinking about how you can read from it on the local machine, but the data you receive can be from any of the clients. A simple to understand analogy is a mailbox. Even if you only have one, anybody can put something in it. Well, usually only a mailman, but you get the point.

Sorry, let me try to verbalize this better. I am watching the Goran multiplayer tutorial here ( ). During the video he has port 9999 to create the socket. However, once the socket is created, I noticed that it’s not on port 9999 but it appeared moreso that port 9999 was a gateway between client socket (which was signed a port number upon socket connection) and server port communication.

Does that make sense? Even though client/server is happening on 9999 (or whatever port is specified), does the Socket create a random/next in line quazi uPnP port to communicate on said socket?

watching the same tutorial (Goaran’s multiplayer one) i wrote this code, also after a close look i realised i got the ip wrong on the client side. so binding on the client side is not nessesary. with this code both computers are acting as clients and servers. as far is i understand it, The important part is that the reciving port must not change. the client must have a designated reciving port and the server must have a desegnated reciving port. 9999 was a convineint free port on both computers. thanks for the reply’s

about the logistics issue. aggose’s page explained it fairly well. there is just a “middle man” with a know ip address for connecting. and the middle man would just send the ip address of the other computer to each client. but i don’t have a computer to designate to connections. and in this case there is no “Server” it seems more efficent to have the clients do their own work right?

If you want to use more than one port you can. For instance, you can wait for clients to send you a something on 9999, then you can send them a message back on 9999 which tells them what port to use for the rest of the conversation. This way you can organize your communications with client better. However, none of this is done by the sockets behind your back, it is something you would have to specify yourself.

So, i had an idea on the whole connecting two computers thing.

so far i was only able to connect to computers on a local network.
usualy computers on a network have the same first few # in their ip. for one case lest use. 192.168
They also have the same third # lets say .32
then they will have the last # be sequental from a start #. so the last # on a series of computers in a network might be:

.100 .101 .102 .103 .104 .105 .106 ect. for every computer on the network

so… you can “search” for other computers trying to connect on the same network, by having both recive input, while they are both brodcasting their ip to ip’s in sequental order.
so computer would send its IP first to and then to and then .104 ect,
and computer would start by sending to .107 and then to .105. each would continue untill they recieved an IP address or a message that would essentaly say “STOP”.
if they recived an IP address: they would then send “STOP” followed by another message that would contain their IP address, to the ip that they received.
if a computer received “STOP” it would wait to receive the IP address of the computer that stoped them.

Now both computers know which IP to send to!
In this way on a local bassis, in less than a minute, (there are realy only 255 possibilities) the computers could connect if they are on the same network.

What i need you to tell me is wether or not this is actualy the case. do all local ip addresses follow the same pattern?

PS: ive actualy tried it just pinging with the CMD in windows, on a public network. i found 2 or three computers on the network this way.

You’re making many assumptions here. This is a brute force method. You are attempting to replicate broadcast addresses

For the internet you will need to have a matchmaking server regardless so it makes sense to use that.