Siess
Siess

Reputation: 23

Making Python game server sockets visible for outside world?

How can i connect by my 80.xxx.xxx.xxx ip (from internet)

My ports are enabled but the game client just dont see any server on 80.xxx.xxx.xxx ip i think the problem is in the server code.

Note: The game client-server connection works perfect on LAN but not over the internet.

Server:

import _thread as thread
import socket
import time
import pickle
import ipaddress
clients = {}
clientInfo = {}
connections = {}

def startServer(nPort):

     
    IP = '192.168.0.10'
    port = nPort
    
    
    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    serverSocket.bind((IP,port))
    serverSocket.setblocking(0)
    print("Server connected "+str(IP)+" port "+str(port))
    return serverSocket
    addSocketTransport
    
def runServer(server):
    recv(server)

def recv(server):
    end = time.time()
    start = time.time()
    ping = 0
    r = ""
    global clients
    server.settimeout(0)
    while(True):
        for i in range(0,len(clients)+1):
            try:
                (r,address) = server.recvfrom(9293)
                r = pickle.loads(r)
                if address not in clients:
                    addClient(address)

                clients[address] = r
                ping = round((time.time()-start)*1000,3)
                start = time.time()
                clientInfo[address]['lastPing'] = time.time()
                clientInfo[address]['ping'] = ping
                clientInfo[address]['timeout'] = 0.0
            except:
                pass
        for c in clients:
            client = clientInfo[c]
            client['timeout'] = round(float(time.time()-client['lastPing'])*1000,3)
            if client['timeout'] > 5000:
                print("the client timed out")
                disconnectClient(c)
                break
    print("follow broke")
def send(clientSocket):
    global clients
    while(True):
        time.sleep(.05)
        for address in clients:
            client = clients[address]
            clientSocket.sendto(pickle.dumps(clients),address)

        


def addClient(address):

    print(str("client add"))
    clients[address] = {}
    clientInfo[address] = {}
    clientInfo[address]['timeout'] = 0.0
    clientInfo[address]['lastPing'] = time.time()
    print(str("clients:")+str(clients))

def disconnectClient(address):
    print(str("disconnect ")+str(address))
    del clients[address]
    print("client disconnected")

go = False
print("What port should be used (Press enter for default 9293 port)")
while go == False:
    port = input("Port: ")
    try:
        port = int(port)
        if port >= 1024:
            go = True
        else:
            print("the port might be bigger than 1024")
    except:
        pass
    if port == "":
        port = 9293
        print("the gateway port 9293")
        go = True

    
#thread.start_new_thread( print, ("Thread-2","Hello") )
try:
    serverSocket = startServer(port)

    thread.start_new_thread( recv, (serverSocket,) )
    thread.start_new_thread( send, (serverSocket,) )
    print("server started")
except Exception as e:
    print (str(e))

killServer = input("Server shutdown? (y/n)")
#
#thread.start_new_thread( send), (serverSocket) )

Upvotes: 1

Views: 429

Answers (2)

Siess
Siess

Reputation: 23

Okay i figured it out myself, thankyou for you two for helping me out! Just the firewall blocked my packets!

Again, Thankyou!

Upvotes: 1

Bartłomiej Bartnicki
Bartłomiej Bartnicki

Reputation: 1175

'192.168.0.10' - this is private IP (http://en.wikipedia.org/wiki/IP_address#Public_and_Private_Addresses) if you want to make your server visible by internet you should configure NAT on your router (only if your WAN adress is public)(http://en.wikipedia.org/wiki/Network_address_translation)

How do it? Easy - you must login on your router by webrowser find NAT configuration and "say" if any think from internet camo on port 1234 you should send it to 192.168.0.10:nPort

Upvotes: 1

Related Questions