LuaSocket 服务器不接受客户端。

我正在编写一个 Lua 脚本在我的 PC 上运行,并需要从我的 Android 连接。因此,在 Lua 脚本中,我只需添加以下内容:

local socket = require 'socket'
local server = socket.tcp()
print("createdserver")
server:bind('*',7070)
print("binded")
while 1 do
  server:listen(32)

  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

而在 Android 端,我使用了一个非常简单的 socket 连接:

public Connection(String ip, int _port) {
    //inMessage = new NetworkMessage();
    buffer = new byte[16394];
    try {
        serverAddres = InetAddress.getByName(ip);
        socket = new Socket(serverAddres, _port);
        socketOut = socket.getOutputStream();
    } catch (IOException e) {
        Log.e("Connect", e.getMessage());
    }
}

在 Android 端,它停留在 “new Socket” 的状态并且没有连接成功。

点赞
用户3747404
用户3747404

我不太熟悉lua,但我的理解是您正在向套接字写入新行,并希望在Android端接收。 通常情况下,如果是这种情况,您需要获取输入流而不是输出流,因为您正在等待结果。 此外,您需要在另一个线程(标准方式)上无限期地(或直到满足某些条件)监听输入流上的数据:

while(true){
    if (inputStreamReader().read() != -1){
          // do you processing
     }
}
2015-04-08 14:17:27
用户945567
用户945567

我的笔记本正在更改其 IP 地址,因此我无法从 Android 访问它,已经解决了!

2015-04-08 14:36:22