将服务器端口传达给客户端? (TCP)
Communicate server port to client? (TCP)
问题
跟随 LuaSocket
Introduction 我设法得到 server
运行。我还设法从 client
端连接。但是要获得此连接,client
必须知道 server
端口号。在示例代码中,server
端口为 0,这意味着:
If port is 0, the system automatically chooses an ephemeral port.
我想这种方法有它的优点,但是可怜的 client
应该如何知道要连接到哪个端口?
问题
如何将临时端口号从 server
传送到 client
?我假设在此过程中不应有任何人为操作。
代码
服务器(来自LuaSocket
Introduction)
-- load namespace
local socket = require("socket")
-- create a TCP socket and bind it to the local host, at any port
local server = assert(socket.bind("*", 0))
-- find out which port the OS chose for us
local ip, port = server:getsockname()
-- print a message informing what's up
print("Please telnet to localhost on port " .. port)
print("After connecting, you have 10s to enter a line to be echoed")
-- loop forever waiting for clients
while 1 do
-- wait for a connection from any client
local client = server:accept()
-- make sure we don't block waiting for this client's line
client:settimeout(10)
-- receive the line
local line, err = client:receive()
-- if there was no error, send it back to the client
if not err then client:send(line .. "\n") end
-- done with client, close the object
client:close()
end
客户(关注 this answer)
local host, port = "127.0.0.1", 100
local socket = require("socket")
local tcp = assert(socket.tcp())
tcp:connect(host, port);
--note the newline below
tcp:send("hello world\n");
while true do
local s, status, partial = tcp:receive()
print(s or partial)
if status == "closed" then break end
end
tcp:close()
How to communicate an ephemeral port number from server to client? I assume there should be no human action in the process.I assume there should be no human action in the process.
你是对的。服务器示例很奇怪。服务器通常应绑定到特定端口。它不应该是短暂的。这样当服务器重新启动时,客户端连接到与之前相同的端口。否则,如果服务器端口不断变化,客户端就会不知所措。
客户端确实可以绑定到临时端口(他们通常这样做)。服务器应绑定到特定服务器。
在 TCP/IP 连接中,服务器始终绑定到已知端口。不允许服务器绑定到端口 0。它必须在已知端口上可用,以便客户端可以连接到它。您应该更改示例以将服务器绑定到固定端口,然后在客户端连接功能中使用该固定端口。
问题
跟随 LuaSocket
Introduction 我设法得到 server
运行。我还设法从 client
端连接。但是要获得此连接,client
必须知道 server
端口号。在示例代码中,server
端口为 0,这意味着:
If port is 0, the system automatically chooses an ephemeral port.
我想这种方法有它的优点,但是可怜的 client
应该如何知道要连接到哪个端口?
问题
如何将临时端口号从 server
传送到 client
?我假设在此过程中不应有任何人为操作。
代码
服务器(来自LuaSocket
Introduction)
-- load namespace
local socket = require("socket")
-- create a TCP socket and bind it to the local host, at any port
local server = assert(socket.bind("*", 0))
-- find out which port the OS chose for us
local ip, port = server:getsockname()
-- print a message informing what's up
print("Please telnet to localhost on port " .. port)
print("After connecting, you have 10s to enter a line to be echoed")
-- loop forever waiting for clients
while 1 do
-- wait for a connection from any client
local client = server:accept()
-- make sure we don't block waiting for this client's line
client:settimeout(10)
-- receive the line
local line, err = client:receive()
-- if there was no error, send it back to the client
if not err then client:send(line .. "\n") end
-- done with client, close the object
client:close()
end
客户(关注 this answer)
local host, port = "127.0.0.1", 100
local socket = require("socket")
local tcp = assert(socket.tcp())
tcp:connect(host, port);
--note the newline below
tcp:send("hello world\n");
while true do
local s, status, partial = tcp:receive()
print(s or partial)
if status == "closed" then break end
end
tcp:close()
How to communicate an ephemeral port number from server to client? I assume there should be no human action in the process.I assume there should be no human action in the process.
你是对的。服务器示例很奇怪。服务器通常应绑定到特定端口。它不应该是短暂的。这样当服务器重新启动时,客户端连接到与之前相同的端口。否则,如果服务器端口不断变化,客户端就会不知所措。
客户端确实可以绑定到临时端口(他们通常这样做)。服务器应绑定到特定服务器。
在 TCP/IP 连接中,服务器始终绑定到已知端口。不允许服务器绑定到端口 0。它必须在已知端口上可用,以便客户端可以连接到它。您应该更改示例以将服务器绑定到固定端口,然后在客户端连接功能中使用该固定端口。