套接字服务器卡住了

时间:2015-01-22 17:37:38

标签: java sockets

我正在尝试让服务器/客户端从客户端向服务器发送文本,然后向客户端发回一条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]);

    }
}

1 个答案:

答案 0 :(得分:1)

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

            writer.print(ret);

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

            String input = reader.readLine();

BufferedReader#readLine的文档说:

  

读取一行文字。一条线被认为是换行(&#39; \ n&#39;),回车(&#39; \ r&#39;)或回车后紧接着换行符中的任何一条终止

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