本文介绍了如何在lua中获得TCP回复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.net.*;
import java.io.*;
public class server {
    public static void main(String args[]) throws Exception {
        ServerSocket server = null;
        Socket client = null;
        PrintStream out = null;
        BufferedReader buf = null;
        server = new ServerSocket(8000);
        client = server.accept();
        buf = new BufferedReader(
                new InputStreamReader(client
                  .getInputStream()));
        out = new PrintStream(
                client.getOutputStream());
        String str = buf.readLine();
        out.println("Echo:"+str);
        System.out.println("HELLO"+str);
        out.close();
        client.close();
    }
};

client.lua

local socket = require("socket")

local host = "127.0.0.1"
local port = 8000
local sock = assert(socket.connect(host, port))
sock:settimeout(0)

print("Press enter after input something:")

local input, recvt, sendt, status
input = io.read()
if #input > 0 then
    assert(sock:send(input .. "\n"))
end
recvt, sendt, status = socket.select({sock}, nil, 1)
local response, receive_status = sock:receive()
print(response)

服务器可以从客户端获取消息,但客户端无法获得回复.

The server can get the messages from the client, but the client can't get the reply.

我可以使用其他语言(如Python或Java)得到答复.

I can get the reply with other languages like Python or Java.

但是我只能通过Lua发送的消息得到Lua的回复.

But I can only get the reply with Lua by the message sent by Lua.

为什么我不能从服务器得到回复?

Why can't I get reply from the server?

推荐答案

您必须将settimeout(0)更改为settimeout(10)并再次测试删除此行recvt, sendt, status = socket.select({sock}, nil, 1)

you must change settimeout(0) to settimeout(10) and test again andremove this line recvt, sendt, status = socket.select({sock}, nil, 1)

这篇关于如何在lua中获得TCP回复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:14