2012-01-26 52 views
10

我正在嘗試爲Lua套接字頁面上的套接字服務器示例製作一個簡單的lua套接字客戶端。lua套接字客戶端

儘管服務器部分工作,我用telnet試過。

但客戶端部分不工作。

local host, port = "127.0.0.1", 100 
local socket = require("socket") 
local tcp = assert(socket.tcp()) 

tcp:connect(host, port); 
tcp:send("hello world"); 

它只是應該連接到它,發送一些數據,並收到一些回報。

有人可以幫我解決嗎?

回答

19

您的服務器很可能每條線路都會收到。如receive文檔中所述,這是默認的接收模式。嘗試添加一個換行符到您的客戶端消息。這完成了在服務器上的接收:

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() 
+1

哇謝謝。那樣做了。 :) – user1058431

+0

或者,使用不同的'接收'模式,例如, '本地三字節= tcp:接收(3)'。 – Phrogz