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

问题描述 投票:-1回答:1

我需要用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个回答
0
投票

你需要提供几个修复方法。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
© www.soinside.com 2019 - 2024. All rights reserved.