我在我的 TCP 客户端-服务器模板上做错了什么?
What i`m doing wrong at my TCP client-server template?
我需要使用 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 )?
您需要提供几个修复:
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
我需要使用 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 )?
您需要提供几个修复:
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