好的,这是今天早些时候修改的问题,我添加了代码来帮助解释问题。我正在从客户端向服务器发送两条消息。然后服务器拾取消息并处理它们。服务器最终尝试将消息发送回客户端(请注意服务器代码“testmessage”),正是在这里我遇到了问题。要么是我没有在客户端收到消息,要么是从服务器端错误地发送了消息。
public class ClientConnection {
String address, language, message;
int portNumber;
Socket clientSocket = null;
public ClientConnection(String lan, String mes, String add, int pn) throws IOException{
address = add;
portNumber = pn;
language = lan;
message = mes;
}
public String createAndSend() throws IOException{
// Create and connect the socket
Socket clientSocket = null;
clientSocket = new Socket(address, portNumber);
PrintWriter pw = new PrintWriter(clientSocket.getOutputStream(),true);
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// Send first message - Message is being correctly received
pw.write(language+"\n");
pw.flush();
// Send off the data
// Send the second message - Message is being correctly received
pw.write(message);
pw.flush();
pw.close();
// Send off the data
// NOTE: Either I am not receiving the message correctly or I am not sending it from the server properly.
String translatedMessage = br.readLine();
br.close();
//Log.d("application_name",translatedMessage); Trying to check the contents begin returned from the server.
return translatedMessage;
}
服务器代码:
public class ServerConnection {
public static void main(String[] args) throws Exception {
// Delete - Using while loop to keep connection open permanently.
boolean status = false;
while( !status){
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
// Delete - Working as of here, connection is established and program runs awaiting connection on 4444
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String language = br.readLine();
String message = br.readLine();
// Test - Works
System.out.println(language);
// Test - Works
System.out.println(message);
// Delete - Working as of here, both messages are passed and applied. Messages are received as sent from client.
TranslateMessage tm = new TranslateMessage();
String translatedMessage = tm.translateMessage(language, message);
// NOTE: This seems to be where I am going wrong, either I am not sending the message correctly or I am not receiving it correctly..
// PrintWriter writer = new PrintWriter(new BufferedOutputStream(clientSocket.getOutputStream()));
PrintWriter pw = new PrintWriter(clientSocket.getOutputStream(),true);
// Send translation back
System.out.println(translatedMessage);
// pw.write(translatedMessage+"\n");
pw.write("Return test"); // Test message!
pw.flush();
// Send off the data
pw.close();
br.close();
clientSocket.close();
serverSocket.close();
}
}
}
代码有点乱,我可以看到一些重复的内容,我已经评论了我觉得问题发生的地方。
感谢您的帮助!
最佳答案
您正在使用 BufferedReader.readLine() 读取服务器的响应,但在测试用例中您发送的字符串不以\n 或\r\n 结尾,因此它不会获取该行据我从文档中可以看出...
public String readLine()
throws IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
额外的建议...
在编写这样的请求响应协议(protocol)时,我不会依赖行结尾来终止请求或响应。通常,我会使用完全格式化的 JSON 字符串,或者我更喜欢二进制协议(protocol),其中所有请求和响应都以二进制计数开头(通常为 4 字节 bigendian/网络字节顺序)。然后客户端和服务器读取 4 个字节,然后读取后面的字节数。这可以处理网络连接上通常发生的数据包 fragment ,还有助于避免恶意用户发送永不终止的长字符串进行 DOS 攻击。
在 Java 中,您可以使用 ByteBuffer.order() 来处理双尾数。
关于java - 客户端和服务器之间使用套接字进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5689667/