Lua客户端,Python服务器,本地一切正常,网络超时

Lua client, Python server, everything's fine in local, timeout on the net

所以,我正在为以撒的结合编写 lua mod。我制作了一个 lua 客户端(使用 lua 套接字)和一个 python 服务器。

在本地,一切正常,但是当我使用 public ip 测试它时(我在路由器上进行了端口转发),lua 客户端在第二次接收时超时。 我不知道是什么原因造成的,日志显示 "timeout at CID"。

编辑(忘记添加): 奇怪的是,对于python服务器来说,就好像他已经发送了一样,因为我在服务器上加超时的时候,只是接收消息“[RCID]\n”超时了。

这里是 lua mod 的网络部分:

local socket = require("socket")
local connectIP = ""
local currentPort = 21666

local loopTimeout = 10
function Network.SendData(data)
    if currentBehaviour == Behaviour.CLIENT then
        client:send(data.."\n")
    end
end
function Network.StartClient()
if currentBehaviour == Behaviour.IDLE then
    client = assert(socket.tcp())
    client:connect(connectIP,currentPort)
    currentBehaviour = Behaviour.CLIENT
    client:settimeout(loopTimeout)
    local seed,errs = client:receive()
    if errs~="timeout" and errs~=nil then
      Network.CloseConnection();
      Isaac.DebugString("seederror:"..errs);
    elseif errs=="timeout" then
      Network.CloseConnection();
      Isaac.DebugString("timeout at seed");
    elseif errs==nil then
      Isaac.DebugString("Seed: : "..seed);
      Isaac.ExecuteCommand("seed "..seed)
    end

    local CID,err = client:receive()
    if err~="timeout" and err~=nil then
      Network.CloseConnection();
      Isaac.DebugString("ciderror:"..err);
    elseif err=="timeout" then
      Network.CloseConnection();
      Isaac.DebugString("timeout at CID");
    elseif err==nil then
      Isaac.DebugString("CID : "..CID);
      ClientID = tonumber(CID)
      Network.SendData("[RCID]")
    end

end
end

这是服务器:

import socket
import select
from parse import *



IsaacClients = {}

seed = b"98BN MJ4D\n"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 21666))
server.listen(10)

num_clients = 0

server_launched = True
connected_clients = []
while server_launched:

    connections_asked, wlist, xlist = select.select([server],
        [], [], 0.05)

    for connection in connections_asked:
        connection_client, info_client = connection.accept()
        print(info_client)

        connected_clients.append(connection_client)
        print("Sending Seed")
        connection_client.sendall(seed) # send the seed
        print("Seed Sent")

        num_clients = num_clients +1
        check = ""
        counter = 0
        while check != "[RCID]\n" and counter<100: # try 100 times to send the ClientID
            print("Sending ClientID")
            try:
                connection_client.settimeout(0.1)
                connection_client.sendall((str(num_clients)+"\n").encode())
                check = connection_client.recv(7).decode() # try to check if it has received it
                connection_client.settimeout(None)
            except:
                pass
            counter=counter+1
            if counter == 100:
                server_launched = False
        print("ClientID Sent")



    clients_to_read = []
    try:
        clients_to_read, wlist, xlist = select.select(connected_clients,
                [], [], 0.05)
    except select.error:
        pass
    else:
        for client in clients_to_read:
            msg_recved = client.recv(1024)
            msg_recved = msg_recved.decode()
            print("[] {}".format(msg_recved))
            if msg_recved.find("[END]")!= -1 :
                server_launched = False
            msg_recved = msg_recved.split('\n') # split into lines

            for line in msg_recved:
                data = parse("[ID]{ID}[POS]{x};{y}[ROOM]{RoomIndex}[CHAR]{CharacterName}[REDM]{MaxHeart}[RED]{Hearts}[SOUL]{SoulHearts}", line)
                if data != None :
                    IsaacClients[data['ID']] = data
            luaTable = "{" # start the generation of the lua table that will be sent
            for player in IsaacClients.values():
                luaTable = luaTable + "[" + player['ID'] +"]={ID=" + player['ID'] + ",POS={x=" +player['x']+ ",y=" +player['y']+ "},ROOM=" +player['RoomIndex']+ ",CHAR='" +player['CharacterName']+ "',REDM=" +player['MaxHeart']+ ",RED=" +player['Hearts']+ ",SOUL=" +player['SoulHearts']+ "}"
            luaTable = luaTable + "}\n"
            print(luaTable)
            print("Sending Table")
            client.sendall(luaTable.encode())
            print("Table Sent")
print("Server closing")
for client in connected_clients:
    client.close()

所以,最后,没有问题,问题是:我的路由器不支持发夹,在尝试使用来自本地网络的 public ip 时会出现奇怪的错误。它在网络外的 PC 上工作正常。