Java Socket Communication" deadlock"

时间:2017-02-13 22:49:26

标签: java multithreading sockets server deadlock

您好我正在尝试使用多个客户端实现服务器操作 问题是服务器没有从输入流接收消息并等到它发生。如果客户端在写入后没有关闭流,服务器将继续等待。客户端发送消息后,他尝试从输入流中读取等待响应,但服务器正在等待请求。所以..死锁

这是我的客户端类

public class Client implements Runnable{

...

@Override
public void run() {

    BufferedReader is = null;
    BufferedWriter os = null;

    try(Socket socket = new Socket(address.getHostName(), address.getPort());){

        String request = String.format("%s-%d-%s",this.destination, this.desiredPlace, this.paymentMethod.toString());
        os = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        is = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        PrintWriter pw = new PrintWriter(os, true);
        pw.write(request);
        pw.flush();
// if I close the stream here the request will be send, but this will close the socket so the I will not receive response.

        String response;
        while ((response = is.readLine()) != null){
            System.out.println(response);
        }

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

这是我的服务器类

 public void perform() throws IOException, DestionationProcessingException, InterruptedException {
    try (ServerSocket server = new ServerSocket(port);) {
        StandalonePayDesk offLinePayDesk = new StandalonePayDesk(ticketManager);
        this.threadPool.submit(offLinePayDesk);

        while (true) {
            Socket socket = server.accept();
            RequestHandler handler = new RequestHandler(this.threadPool, offLinePayDesk, this.ticketManager);
            handler.process(socket);
        }
    }
}

和RequestHandler类用于处理每个客户端

try (BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintWriter writer = new PrintWriter(client.getOutputStream(), true)) {

        writer.println("hello");
        writer.flush();

        String line;
        while ((line = reader.readLine()) != null) {

            String[] lineTokens = line.split("-");
              ...

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

pw.write(request);

您的客户正在撰写请求但不是一行。您的服务器正在使用readLine()读取一行,并将阻塞直到行终止符到达,这是永远不会发生的,因此它永远不会发送回复,因此您的客户端永远不会收到它,因此它将永远阻止。 / p>

将上述内容更改为:

pw.println(request);