Communication to Different Networks
<p>I would like to make a game with python and PyGame where two players play via wi-fi on different networks networks. I currently have this code (which I got from a video). </p>

<pre><code># SERVER

import socket

def Main():
    host = '127.0.0.1'
    port = 5000

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((host,port))


    print("Server Started.")
    while True:
        data, addr = s.recvfrom(1024)
        data = data.decode('utf-8')
        print("message From: " + str(addr))
        print("from connected user: " + data)
        data = data.upper()
        print("sending: " + data)
        s.sendto(data.encode('utf-8'), addr)
    c.close()

if __name__ == '__main__':
    Main()

# CLIENT

import socket

def Main():
    host = '127.0.0.1'
    port = 5001

    server = ('127.0.0.1',5000)

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((host, port))

    message = raw_input("-&gt; ")
    while message != 'q':
        s.sendto(message.encode('utf-8'), server)
        data, addr = s.recvfrom(1024)
        data = data.decode('utf-8')
        print('Received from server: ' + data)
        message = raw_input("-&gt; ")
    s.close()

if __name__ == '__main__':
    Main()
</code></pre>

<p>This works fine on the same machine. How could I make this work on two different computers (on two different LAN)?</p>

<p>change to <code>host= '0.0.0.0'</code> (for the server)</p>

<p>this makes it publish to any available interface ... if you have a router you (probably) will also need to use the port-forward settings of your router to direct traffic to the correct computer</p>

<p>as an aside ... what is this nonsense? <code>server = ('127.0.0.1',5000)</code></p>

<p>for the client ... just set the public facing IP address of your network with the appropriate port(if you are using portforwarding) ... you canfind this ip address @ <a href="http://whatismyip.com" rel="nofollow">http://whatismyip.com</a></p>

python networking udp