我试图通过Socket发送两个号码。服务器接收号码并进行一些计算,但当我向客户发送结果时,客户端会收到一个他发送的号码。
我做错了因为我不理解?
Client.java
public class Client {
private static Socket socket;
public static void main(String args[]) {
try {
String host = "localhost";
int port = 25010;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String number = "2";
String number2 = "5";
String sendMessage = number + "\n";
String sendMessage2 = number2 + "\n";
bw.write(sendMessage);
bw.write(sendMessage2);
bw.flush();
System.out.println("Message sent to the server:\n" + sendMessage + sendMessage2);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " + message);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
//Closing the socket
try {
socket.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
Server.java
public class Server {
private static Socket socket;
public static void main(String[] args) {
try {
int port = 25010;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port " + port);
ArrayList<String> arr = new ArrayList<String>();
//Server is running always. This is done using this while(true) loop
while(true) {
//Reading the message from the client
socket = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); // primeste mesaj de la client
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); // transmite raspuns catre client
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String inputLine;
while ((inputLine = br.readLine()) != null) {
out.println(inputLine);
arr.add(inputLine.trim());
}
System.out.println("Message received from client is:");
for (int i = 0; i < arr.size(); i++) {
System.out.println(arr.get(i));
}
//Return message
String returnMessage = null;
try {
int numberInIntFormat = 0;
int num = 1;
for (int i = 0; i < arr.size(); i++) {
System.out.println(arr.get(i));
numberInIntFormat = Integer.parseInt(arr.get(i));
num = num * numberInIntFormat;
}
arr.clear();
returnMessage = String.valueOf(num);
} catch(NumberFormatException e) {
//Input was not a number. Sending proper message back to client.
returnMessage = "Please send a proper number\n";
}
//Sending the response back to the client.
bw.write(returnMessage);
bw.flush();
System.out.println("returnMessage = " + returnMessage);
System.out.println("Message sent to the client is "+ returnMessage);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch(Exception e) {}
}
}
}
答案 0 :(得分:0)
您的服务器代码在它执行任何其他操作之前回应它从客户端读回的所有内容:
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); // primeste mesaj de la client
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); // transmite raspuns catre client
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String inputLine;
while ((inputLine = br.readLine()) != null) {
out.println(inputLine); // <-- HERE
arr.add(inputLine.trim());
}
客户端收到服务器发送的内容并不足为奇。
答案 1 :(得分:0)
String inputLine;
while ((inputLine = br.readLine()) != null) {
arr.add(inputLine.trim());
}
通过此更改,输出为:
服务器强>
Server Started and listening to the port 25010
<强>客户端:强>
Message sent to the server:
2
5
现在服务器在while循环中被“阻止”,而客户端没有收到任何反馈。
答案 2 :(得分:0)
试试这个:
客户端:
public class Client {
private static Socket socket;
public static void main(String args[]) {
try {
String host = "localhost";
int port = 25010;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String number = "2";
String number2 = "5";
String sendMessage = number + "\n";
String sendMessage2 = number2 + "\n";
bw.write(sendMessage);
bw.write(sendMessage2);
bw.newLine(); // You need to send a special line for say to the server: "Hey, I have done";
bw.flush();
System.out.println("Message sent to the server:\n" + sendMessage + sendMessage2);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " + message);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
//Closing the socket
try {
socket.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
服务器:
public class Server {
private static Socket socket;
public static void main(String[] args) {
try {
int port = 25010;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port " + port);
ArrayList<String> arr = new ArrayList<String>();
//Server is running always. This is done using this while(true) loop
while(true) {
//Reading the message from the client
socket = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); // primeste mesaj de la client
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); // transmite raspuns catre client
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String inputLine;
while ((inputLine = br.readLine()) != null && inputLine.length() > 0) { // You need to stop loop when you get empty line
// out.println(inputLine);
arr.add(inputLine.trim());
System.out.println("Message received from client is:"+inputLine.trim());
}
System.out.println("Message received from client is:");
//Return message
String returnMessage = null;
try {
int numberInIntFormat = 0;
int num = 1;
for (int i = 0; i < arr.size(); i++) {
System.out.println(arr.get(i));
numberInIntFormat = Integer.parseInt(arr.get(i));
num = num * numberInIntFormat;
}
arr.clear();
returnMessage = String.valueOf(num);
} catch(NumberFormatException e) {
//Input was not a number. Sending proper message back to client.
returnMessage = "Please send a proper number\n";
}
//Sending the response back to the client.
bw.write(returnMessage+"\n"); // You need to add '\n' otherwise readLine never gets;
bw.flush();
System.out.println("returnMessage = " + returnMessage);
System.out.println("Message sent to the client is "+ returnMessage);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch(Exception e) {}
}
}
}
答案 3 :(得分:-1)
我找到了一个更好的方法:
while (br.ready() && (inputLine = br.readLine()) != null)
这告诉我要读取缓冲区是否需要阅读。
根据@John Bollinger的响应,缓冲区只读取到第一个换行符,所以如果你试图解析一个包含换行符的String,你会在出现换行符时离开。
使用br.ready(),它将解析所有String,并在缓冲区结束时退出。