套接字服务器卡住

Socket server stuck

我正在尝试 server/client 将文本从客户端发送到服务器,然后将 ok 消息或类似的消息发回客户端,但是对于一些我看不到的错误,服务器在将 ok 发送回客户端之前卡住了,或者客户端没有收到消息(我认为这是第一个)。 感谢任何帮助。

这是服务器代码:

class ActiveServer extends Thread {

    InputStream inStream;
    OutputStream outStream;

    public ActiveServer(InputStream inStream, OutputStream outStream) {
        this.inStream = inStream;
        this.outStream = outStream;
    }

    @Override
    public void run() {
        boolean ret = false;
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
                PrintWriter writer = new PrintWriter(outStream);) {
            String line = null;
            while((line = reader.readLine()) != null) {
                String[] str = line.split(";");
                line = null;
                switch (str[0]) {
                case "insert" : //ret = SQLOptions.insert(str[1], str[2]);
                    System.out.println(str[1]);
                    break;
                }
                writer.print(ret);
                writer.flush();
                // As far as i can see it gets stuck at the end of this while, but I don't know why.
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

public class Server {

    private static final int PORT = 39165;

    public static void main(String[] args) {

        try (ServerSocket server = new ServerSocket(PORT);) {
            System.out.println("Servidor online");
            ExecutorService service = Executors.newFixedThreadPool(10);
            while (true) {
                Socket client = server.accept();
                InetAddress ip = client.getInetAddress();
                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                Date time = new Date();
                System.out.print(sdf.format(time));
                System.out.println(" " + ip + " connected");
                InputStream inStream = client.getInputStream();
                OutputStream outStream = client.getOutputStream();
                service.execute(new ActiveServer(inStream,outStream));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

客户端代码如下:

public class Telnet {

    static Console console = System.console();

    public static void connect(String ip, String port) {
        try(Socket socket = new Socket(ip, Integer.parseInt(port));
                PrintWriter writer = new PrintWriter(socket.getOutputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));) {

            String msg = null;
            while(true) {
                msg = console.readLine();
                writer.println(msg);
                writer.flush();

                if (msg.equals(".quit")) {
                    System.out.println("Exiting...");
                    break;
                }

                String input = reader.readLine();
                System.out.println(input);
            }


        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        if(args.length < 2) {
            err.println("Telnet <ip> <port>");
            return;
        }

        if (console == null) {
            err.println("A console is not available");
            return;
        }

        connect(args[0], args[1]);

    }
}

在服务器端,您编写没有终止换行符的响应:

            writer.print(ret);

但是在客户端,你一直读到行尾:

            String input = reader.readLine();

BufferedReader#readLine 的文档说:

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

因此,客户端将永远等待服务器永远不会发送的换行序列。