![](/img/trans.png)
[英]How to pause a specific string after it has been sent from client to server in java?
[英]How should a client properly wait for a reponse from the server after a message has been sent?
我正在嘗試使客戶端和服務器正確回顯消息。
這意味着客戶端應該向服務器發送一條消息,服務器應該接收並發送相同的消息給客戶端,然后客戶端打印出接收到的消息。
我無法讓客戶端正確等待服務器完成發送響應,然后客戶端接收並打印它。
它起作用的唯一方法是當我注釋掉 EchoClient2.java RECEIVE MESSAGE 代碼時。
EchoClient2.java
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
public class EchoClient2 {
private Socket clientSocket;
private DataOutputStream out;
private DataInputStream in;
private void start(String ip, int port) {
try {
clientSocket = new Socket(ip, port);
out = new DataOutputStream(clientSocket.getOutputStream());
in = new DataInputStream(clientSocket.getInputStream());
} catch (IOException e) {
System.out.println("Error when initializing connection");
}
}
private void sendMessage(String message) throws IOException {
//SENDING MESSAGE
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
System.out.println("Client will send the text: " + message);
out.write(messageBytes);
out.flush();
//RECEIEVE MESSAGE - Doesn't give server a chance to respond?
byte[] data = new byte[8];
ByteArrayOutputStream getData = new ByteArrayOutputStream();
int numBytes;
while ((numBytes = in.read(data)) != -1) {
getData.write(data, 0, numBytes);
}
byte[] message2Bytes = getData.toByteArray();
String text = new String(message2Bytes, StandardCharsets.UTF_8);
System.out.println("Server sent the client text: " + text);
}
private void stop() {
try {
in.close();
out.close();
clientSocket.close();
} catch (IOException e) {
System.out.println("error when closing");
}
}
public static void main(String[] args) throws IOException {
EchoClient2 client = new EchoClient2();
client.start("127.0.0.1", 4444);
client.sendMessage("exit");
client.stop();
}
}
EchoServer2.java
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.io.*;
public class EchoServer2 {
private ServerSocket serverSocket;
private Socket clientSocket;
private DataOutputStream out;
private DataInputStream in;
public void start(int port) {
try {
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();
out = new DataOutputStream(clientSocket.getOutputStream());
in = new DataInputStream(clientSocket.getInputStream());
} catch (IOException e) {
System.out.println("Error when establishing/accepting server connection");
}
}
private void actionMessage() throws IOException {
//RECEIEVE MESSAGE
byte[] data = new byte[8];
ByteArrayOutputStream getData = new ByteArrayOutputStream();
int numBytes;
while ((numBytes = in.read(data)) != -1) {
getData.write(data, 0, numBytes);
}
byte[] messageBytes = getData.toByteArray();
String text = new String(messageBytes, StandardCharsets.UTF_8);
System.out.println("Server got " + text + " and will send this back to client");
//SENDING BACK SAME MESSAGE
out.write(messageBytes);
out.flush();
}
public void stop() {
try {
in.close();
out.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) throws IOException {
EchoServer2 server = new EchoServer2();
System.out.println("Now waiting for incoming connection...");
server.start(4444);
server.actionMessage();
server.stop();
}
}
有誰知道為什么這不能正常工作?
服務器正在以 8 字節的塊讀取客戶端的輸入,直到客戶端斷開連接(它沒有這樣做),然后它正在寫回響應。 服務器需要在每個塊到達時寫回,然后在客戶端斷開連接后斷開連接。 這意味着在while
讀取循環中移動out.write()
。
private void actionMessage() throws IOException {
byte[] data = new byte[8];
int numBytes;
while ((numBytes = in.read(data)) != -1) {
out.write(data, 0, numBytes);
out.flush();
}
}
這對於簡單的回顯服務器來說很好,但對於更現實的消息傳遞服務器來說不是很有用。 您需要一個更明確的協議來來回交換消息,而不必丟失它們之間的連接(除非您想實現無狀態協議,例如 HTTP,它允許請求之間斷開連接)。 例如:
客戶:
private void sendMessage(String message) throws IOException {
System.out.println("Client will send the text: " + message);
//SEND MESSAGE
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
out.writeInt(messageBytes.length);
out.write(messageBytes);
out.flush();
//RECEIVE MESSAGE
int len = in.readInt();
byte[] data = new byte[len];
in.readFully(data);
String text = new String(data, StandardCharsets.UTF_8);
System.out.println("Server sent the client text: " + text);
}
public static void main(String[] args) throws IOException {
EchoClient2 client = new EchoClient2();
client.start("127.0.0.1", 4444);
try {
client.sendMessage("hello");
client.sendMessage("exit");
} catch (IOException e) {
...
}
client.stop();
}
服務器:
private void actionMessage() throws IOException {
//RECEIVE MESSAGE
int len = in.readInt();
byte[] data = new byte[len];
in.readFully(data);
String text = new String(data, StandardCharsets.UTF_8);
System.out.println("Server got " + text + " and will send this back to client");
//SEND MESSAGE
data = text.getBytes(StandardCharsets.UTF_8);
out.writeInt(data.length);
out.write(data);
out.flush();
}
public static void main(String[] args) throws IOException {
EchoServer2 server = new EchoServer2();
System.out.println("Now waiting for incoming connection...");
server.start(4444);
while (true) {
try {
server.actionMessage();
} catch (IOException e) {
...
break;
}
}
server.stop();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.