我有一个由客户端和服务器组成的Java应用程序。客户端向服务器发送加密命令,服务器执行它们。Java:从套接字获取多行
现在我遇到的问题是,使用我的加密算法,有时加密的命令包含“\ n”或“\ r”字符,这些字符混淆了我的服务器代码。这是因为我正在使用readLine()方法,该方法在找到行终止符时停止。我需要的是读取客户端发送到一个字符串中的所有字符的方法。
这里是我的代码:
public void run(){
System.out.println("Accepted Client!");
try{
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "ISO8859_1"));
out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(), "ISO8859_1"));
String clientCommand = null;
while(RunThread){
// read incoming stream
do{
clientCommand = in.readLine();
}while(clientCommand == null);
//decrypt the data
System.out.println("Client: " + clientCommand);
if(clientCommand.equalsIgnoreCase("quit")){
RunThread = false;
}else{
//do something
out.flush();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
一切我已经试过(各种形式使用read()函数的嵌套循环)没有奏效。我会欢迎任何帮助或建议。感谢大家!
只是不要使用文本,Reader/Writer,而是使用Input/OutputStream,并读取二进制数据(字节)。在上面的代码中,编码可能会造成严重破坏。 –