java - 我的 TCP 客户端-服务器模板做错了什么?

标签 java tcp client-server bufferedreader

我需要使用 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 )? 

最佳答案

您需要提供多项修复: 服务器

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

客户端不要忘记发送新行两次 - 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

关于java - 我的 TCP 客户端-服务器模板做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61797553/

相关文章:

java - 在 if 语句中使用字符串数组的值

用于生成交互式图形的 Java 库

java - 如何在不读取逗号的情况下从字符串中读取 double 和整数

asp.net - IIS 只监听 127.0.0.1 而不是 0.0.0.0

c# - C# 中的 Winsock 服务器/客户端应用程序

java - 用于权限异常的内置java异常类型

c++ - 如何安全删除 QT::QTcpSocket?

sockets - Solaris10中如何发送ACK包

c# - 多个客户端在服务器上调用函数 : Isolate each client calls

c# - 保护通过网络发送的服务器数据