【问题标题】:What i`m doing wrong at my TCP client-server template?我的 TCP 客户端-服务器模板做错了什么?
【发布时间】:2020-08-30 23:58:48
【问题描述】:

我需要使用 TCP 套接字做一个简单的 ADD 应用程序,但它不起作用。我做错了什么? 我已经尝试了很多方法,以至于我想我错过了一些东西,我不知道是什么。你能帮助我一些你的知识吗? :)

public class Server {

    static int total;
    static int st;
    static int nd;

    public static int sum(int x, int y){
        return x + y;
    }

    public static void main(String[] args) {

        try(ServerSocket appServer = new ServerSocket(631);
                Socket appSocket = appServer.accept();
                BufferedReader inStream = new BufferedReader(new InputStreamReader(appSocket.getInputStream()));
                BufferedOutputStream outStream = new BufferedOutputStream(appSocket.getOutputStream())){

            String line = inStream.readLine();
            while(line!=null&&line.equals("")){
                st = Integer.parseInt(line.split(" ")[0]);
                nd = Integer.parseInt(line.split(" ")[1]);
                total = sum(st,nd);
            }

            String out = String.valueOf(total);
            outStream.write(out.getBytes());
        } catch(IOException e){
            System.out.println("Server failed.");
            System.out.println(e.getMessage());
        }
    }

}

and those from client:

    public static void main(String[] args) {

        String numbers = "1 2";

        try(Socket client = new Socket("localhost", 631);
                BufferedReader bis = new BufferedReader(new InputStreamReader(client.getInputStream()));
                BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream())){
                System.out.println("Client connected. Waiting for ADD numbers");
                bos.write(numbers.getBytes());
                bos.flush();

                String res = bis.readLine();
                while((res = bis.readLine()) != null){
                    System.out.println("The result is " + res);
                }
        } catch (IOException ex) {
            System.out.println("Connection Problem. " + ex.getMessage());
        }
    }
}

Why this doesn't run as expected ( The result is 3 )? 

【问题讨论】:

    标签: java tcp client-server bufferedreader


    【解决方案1】:

    您需要提供几个修复: Server

    String line;
    while((line = inStream.readLine()) != null ) { // server was blocked here until you send empty line
        if (line.isBlank()) {
            break;
        }
        st = Integer.parseInt(line.split(" ")[0]);
        nd = Integer.parseInt(line.split(" ")[1]);
        total = sum(st,nd);
    }
    
    String out = total + "\n";  // << send total with the new-line so that the client could read it
    
    

    Client 不要忘记发送新行两次 - 1 提交号码和 2 - 让服务器知道数据传输完成并且可以退出它的阅读循环。

    numbers += "\n\n";
    bos.write(numbers.getBytes());
    bos.flush();
    
    String res;  // remove reading before the loop to avoid swallowing the server result
    while((res = bis.readLine()) != null){
        System.out.println("The result is " + res);
    }
    

    更新 来自服务器的调试日志:

    2020-05-15T08:31:38.772917900Z: Server started
    2020-05-15T08:31:43.280246800Z: Client connected
    2020-05-15T08:31:43.313244900Z: Read line from client '1 2'
    2020-05-15T08:31:43.313244900Z: Parsing data in the input
    2020-05-15T08:31:43.324246400Z: Total calculated: 3
    2020-05-15T08:31:43.324246400Z: Read line from client ''
    2020-05-15T08:31:43.324246400Z: Reading from client finished, writing response
    2020-05-15T08:31:43.329244500Z: Sent response to client
    2020-05-15T08:31:43.330245900Z: Server finished, bye
    

    来自客户端的调试日志:

    2020-05-15T08:31:43.240248Z: Client started, numbers = '1 2'
    2020-05-15T08:31:43.289246300Z: Sending request to the server, numbers='1 2\n\n'
    2020-05-15T08:31:43.291246100Z: Request sent
    Waiting for ADD numbers
    The result is 3
    2020-05-15T08:31:43.334246400Z: Client finished
    

    【讨论】:

    • Alex,我尝试使用提供的修复程序运行,但客户端进程仍然卡住(它没有结束 + 不显示结果)。可能是什么问题?
    • 您好,请仔细检查您是否提供了所有修复,并了解如何使用您最喜欢的 IDE 或至少通过将调试消息打印到控制台来调试应用程序。我正在使用来自客户端和服务器的完整调试日志更新我的答案,以证明它们按预期工作。
    • @alender,我刚刚重新复制了您的代码并应用了我的补丁 - 一切正常!很可能,您在粘贴补丁时犯了一些小错误。
    • 感谢您的解决方案,现在我学到了一些新东西!它工作得很好:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    相关资源
    最近更新 更多