gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:45:05 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com