Java TCP 客户端-服务器 - 在两个应用程序中都陷入无限循环

Java TCP Client-Server - stuck in infinite loops in both applications

我有一个客户端和一个服务器,它们应该可以通过两种方式进行通信。一切正常,客户端向服务器发送了一些信息,服务器用这些信息做了一些事情。现在我尝试实现服务器回复。在我尝试实现之后,两个程序现在都陷入了无限循环,等待来自另一端的信息。

这是我的服务器端代码:

    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {

    logger.log(Level.INFO, "args[0]: {0} args[1]: {1} args[2]: {2} args[3] {3}", new Object[]{args[0], args[1], args[2], args[3]});

    pathToExcel = args[0];
    pathToDatabase = args[1];
    numberOfAccounts = Integer.parseInt(args[2]);
    portNumber = Integer.parseInt(args[3]);

    listIE = new ArrayList<>();

    listIE = Loader.getList(numberOfAccounts, pathToExcel);

    DBBroker.createTables(pathToDatabase);
    System.out.println("Check value: " + DBBroker.checkDB());
    if (DBBroker.checkDB() == false) {
        DBBroker.insertData();
        DBBroker.insertDataBalance();
    } else {
        System.out.println("Data has already been inserted into the database");
    }
    startServer();
}

public static void startServer() throws IOException {
    //ServerSocket ss = new ServerSocket(portNumber);
    ServerSocket ss = new ServerSocket(portNumber);
    logger.log(Level.INFO, "Server started on port number: {0}", portNumber);
    try {
        while (true) {
            Socket clientSocket = ss.accept();
            BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            DataOutputStream clientOutput = new DataOutputStream(clientSocket.getOutputStream());
            System.out.println("Client connected ");
            //***************************************************************************
            String answer = input.readLine();
            //***************************************************************************
            System.out.println("prosao readline");

            //logger.info("Client logged in on port " +portNumber);

            String[] niz = answer.split("_");
            //System.out.println("niz: " +Arrays.toString(niz));
            serverPortNumber = Integer.parseInt(niz[0]);
            accountName = niz[1];
            receiverName = niz[2];
            amount = Integer.parseInt(niz[3]);
            //System.out.println("Server port number: " +serverPortNumber + " accountname: " +accountName +" receiver name: " +receiverName + " amount: " +amount);
            parseRequestFromClient();

            System.out.println("Prosao request");
            
            
            clientOutput.writeBytes("Kraj");
            clientSocket.close();

        }
        //ss.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是我的客户端代码:

    public static void main(String[] args) throws IOException {
    String messageFromServer = "";
    logger.log(Level.INFO, "args[0]: {0} args[1]: {1} args[2]: {2} args[3] {3}", new Object[]{args[0], args[1], args[2], args[3]});
    Socket socket = new Socket("localhost", Integer.parseInt(args[0]));
    //logger.info("args[0]: " +args[0] +" args[1]: " +args[1] +" args[2]: " +args[2] +" args[3] " +args[3]);
    
    DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
    BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));


    String dataForServer = args[0]+"_"+args[1]+"_"+args[2]+"_"+args[3]; 
    System.out.println("Data for server: " +dataForServer);
    outputStream.writeBytes(dataForServer);
    System.out.println("prosao dataforserver");

    //***************************************************************************
    String answer = input.readLine();
    //***************************************************************************
    
    System.out.println("prosao readline");
    System.out.println(answer);
    
    socket.close();
}

服务器端卡在 ss.accept() 行,而客户端卡在 input.readLine()

我没有添加整个项目,因为其中很大一部分与问题无关,而且代码很多。

您的服务器在 readLine() 上阻塞。它会阻塞行终止符。客户端只发送一个原始字符串。解决方案是为每个原始字符串发送一个行终止符。 服务器响应客户端时也是如此。

正如 Simon 指出的那样,如果您的消息协议要传递行终止字符串,则印刷机将是一个不错的选择。