ソケットを介して通信するにはクライアントとサーバーが必要です。クライアントは特定のメッセージ(文字列 "arquivo"を通して)を要求し、10KBの長さになっているメッセージを取得することをサーバーに伝えます。クライアントが同じ文字列でメッセージを再度要求したい場合は、両方とも接続を開いたままにしておく必要があります。私は1交換のためにそれを実装して、接続を閉じることができます。しかし、私はしばらく置くと、サーバーは明らかにメッセージを送信しますが、クライアントが実行されているターミナルウィンドウを閉じない限り、クライアントには決して到達しません。別々のターミナルウィンドウでそれぞれを実行し、メッセージを一度だけ送信してから接続を終了してメッセージが送信されるサーバーでクライアントのコードをテストしました。次のように問題のあるサーバーのコードは次のとおりです。ループを使用してメッセージを複数回送受信するときのLua TCPサーバーの問題
またfilename = "file10KB.txt"
file = assert(io.open(filename, "r"))
msg = file:read("*a")
file:close()
socket = require("socket")
host = "*"
port = 8080
print("Binding to host '" .. host .. "' and port " .. port .. "...")
server = assert(socket.bind(host, port))
server:setoption("reuseaddr", true)
ipaddress, socketport = server:getsockname()
assert(ipaddress, socketport)
print("Waiting connection from client on " .. ipaddress .. ":" .. socketport .. "...")
connection = assert(server:accept())
connection:setoption("keepalive", true)
connection:settimeout(10)
print("Connected!")
while true do
print("Waiting for request...")
data, errormsg = connection:receive()
if data == "arquivo" then
print("Proper request received, sending message...")
assert(connection:send(msg .. "\n"))
print("Message sent!")
elseif not errormsg then
print("Invalid request.")
break
else
print("Error message: " .. errormsg)
break
end
end
、私のクライアントのコード:
socket = require("socket")
host = "localhost"
port = 8080
print("Attempting connection to server '" .. host .. "' and port " .. port .. "...")
client = assert(socket.connect(host, port))
client:setoption("keepalive", true)
client:setoption("reuseaddr", true)
client:settimeout(10)
print("Connected!")
repeat
print("Sending message request...")
assert(client:send("arquivo" .. "\n"))
print("Message request sent! Waiting for message...")
data, errormsg = client:receive("*a")
if data then
print("Message received!")
print(data)
else
print("Failed to receive message.")
break
end
until not client
私はLuaのへとソケットへの新たなんだと私はウェブ上で発見いくつかのチュートリアルなどを追いましたhttp://www.portugal-a-programar.pt/topic/36175-aprendendo-a-usar-lua-socket/(ポルトガル語)。私はあなたの誰かが私が行方不明になっていることに光を当てることができることを願っています。 Mac OS Xマシンを使用していますが、それが役に立つかどうかわかりません。私もしばらく置くとき
ありがとうございます、私はそれらを修正します。また、私の問題が何であるかを考え、以下の質問に答えます。再度、感謝します! – eweiner