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

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

因此,我正在为lua mod编码以撒的绑定。 我制作了一个lua客户端(使用luasocket)和一个python服务器。

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

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

这是lua mod的网络部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
local socket = require("socket")
local connectIP =""
local currentPort = 21666

local loopTimeout = 10
function Network.SendData(data)
    if currentBehaviour == Behaviour.CLIENT then
        client:send(data.."\
"
)
    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

这是服务器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import socket
import select
from parse import *



IsaacClients = {}

seed = b"98BN MJ4D\
"


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]\
"
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)+"\
"
).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('\
'
) # 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 +"}\
"

            print(luaTable)
            print("Sending Table")
            client.sendall(luaTable.encode())
            print("Table Sent")
print("Server closing")
for client in connected_clients:
    client.close()

因此,最后没有问题了,问题是:我的路由器不支持发夹,在尝试使用本地网络中的公共IP时出现了奇怪的错误。 它可以在网络之外的PC上正常工作。