Question

server.java

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.

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

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

Why can't I get reply from the server?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top