如何通过我的80.xxx.xxx.xxx ip(来自互联网)
进行连接我的端口已启用,但游戏客户端只是在80.xxx.xxx.xxx ip上看不到任何服务器我认为问题出在服务器代码中。
注意:游戏客户端 - 服务器连接在局域网上运行良好,但不能通过互联网完成。
服务器:
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) )
答案 0 :(得分:0)
' 192.168.0.10' - 这是私人IP(http://en.wikipedia.org/wiki/IP_address#Public_and_Private_Addresses) 如果您想通过互联网查看您的服务器,您应该在路由器上配置NAT(仅当您的WAN地址是公开的)(http://en.wikipedia.org/wiki/Network_address_translation)
怎么做?简单 - 您必须通过webrowser登录您的路由器才能找到NAT配置并且"说"如果有任何想法从端口1234上的互联网迷彩你应该发送到192.168.0.10:nPort
答案 1 :(得分:0)
好吧,我自己想出来了,谢谢你们两位帮助我的人! 只是防火墙阻止了我的数据包!
再次,谢谢!