我正在使用 Java Networking API 创建客户端/服务器。我的客户端会在消息前后向服务器发送特殊的 unicode 字符。在消息之前它将发送\uc001B
和消息之后\uc00C
。再次成功发送消息后,客户端将发送\r
到服务器。服务器可以通过接收到这个来识别消息发送完成。但我的问题是如何在服务器中检查来自客户端的消息是否有\r
.
DataOutputStream outToServer = new DataOutputStream( clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
outToServer.writeBytes("\uc001B");
outToServer.flush();
outToServer.writeBytes(message.toString());
outToServer.writeBytes("\uc001C");
outToServer.flush();
outToServer.writeBytes("\r");
outToServer.flush();
这是我的服务器代码,用于从客户端读取消息
in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter( m_clientSocket.getOutputStream()));
String receivingMessage = "";
while (m_bRunThread) {
String clientCommand = in.readLine().toString();
receivingMessage += clientCommand;
System.out.println("Client Says :" + clientCommand);
if (in.equals("\r")) {
System.out.print("Message Receiving from Client Done : "+ m_clientID);
m_bRunThread = false;
}
}
谢谢