我现在遇到了这个问题。 对于这个问题,我必须:通过套接字压缩和解压缩数据
客户端:压缩从控制台的各输入线,将其发送到服务器和从服务器
服务器端解压缩每个消息:解压缩从客户端的数据,改变低 - 将大写字母压缩并发送回客户端
我能做的最好的事情就是只用一行就可以完成上述任何操作。
客户端:
/* SOCKET CONNECTING STUFF UP HERE */
/*PROBLEMS START AROUND HERE */
String line;
BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));
DeflaterOutputStream compress = new DeflaterOutputStream(socket.getOutputStream(), true);
InflaterInputStream decompress = new InflaterInputStream(socket.getInputStream());
BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));
line = bis.readLine();
line = line + "\n";
compress.write(line.getBytes(), 0, line.length());
compress.finish();
System.out.println("Message sent: " + line);
System.out.println("Message Returned : " +fromClient.readLine());
/* closing the streams here */
bis.close();
decompress.close();
compress.close();
fromClient.close();
socket.close();
}
}
服务器端:
String line = "";
OutputStream outstream = new FileOutputStream("compessserver.txt");
InflaterInputStream decompress = new InflaterInputStream(clientsocket.getInputStream());
BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));
DeflaterOutputStream compress = new DeflaterOutputStream(clientsocket.getOutputStream());
while ((line = fromClient.readLine()) != null) {
String upperLine = line.toUpperCase();
System.out.println("Message received and converted: " + upperLine);
System.out.println();
upperLine = upperLine + "\n";
byte[] input = upperLine.getBytes();
outstream.write(input);
outstream.write("\r\n".getBytes());
compress.write(input);
System.out.println("Message returned : " + upperLine);
compress.finish();
if (upperLine.equalsIgnoreCase("x")) {
break;
}
}
decompress.close();
compress.close();
fromClient.close();
outstream.close();
socket.close();
}
}
我真的需要帮助,在此,请。如果我尝试使这个多输入,而整个代码只是混淆了。在这里呆了好几天。
编辑:忘了提这个。我应该做的是输入一行,压缩它,发送到服务器,服务器解压缩它和大写字母,压缩它,发送回客户端。然后,我应该投入更多的线路,直到我把一个英文字母,如“Q”这情况下,结束程序
我尝试下面的代码,使之成为多行
第二次尝试的客户端工作:
String line;
BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));
DeflaterOutputStream compress = new DeflaterOutputStream(socket.getOutputStream(), true);
InflaterInputStream decompress = new InflaterInputStream(socket.getInputStream());
BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));
line = bis.readLine();
while ((!line.equalsIgnoreCase("x"))) {
compress.write(line.getBytes(), 0, line.length());
System.out.println("Message sent: " + line);
System.out.println("Message returned:" +fromClient.readLine());
line = bis.readLine();
}
bis.close();
fromClient.close();
socket.close();
}
}
第二次尝试服务器端:
OutputStream outstream = new FileOutputStream("compessserver.txt");
InflaterInputStream decompress = new InflaterInputStream(clientsocket.getInputStream());
BufferedReader fromClient = new BufferedReader(new InputStreamReader(decompress));
DeflaterOutputStream compress = new DeflaterOutputStream(clientsocket.getOutputStream());
while ((line = fromClient.readLine()) != null) {
String upperLine = line.toUpperCase();
System.out.println("Message received and converted: " + upperLine);
System.out.println();
upperLine = upperLine + "\n";
byte[] input = upperLine.getBytes();
outstream.write(input);
outstream.write("\r\n".getBytes());
compress.write(input);
System.out.println("Message returned : " + upperLine);
if (upperLine.equalsIgnoreCase("x")) {
break;
}
}
decompress.close();
fromClient.close();
outstream.close();
socket.close();
}
}
问题是什么?你期望发生什么?会发生什么呢? – cubrr
那么,如果我在控制台上输入一行代码,我只能让它工作。我应该做的是输入一行,压缩它,发送到服务器,服务器解压缩它和大写字母,压缩它,发送回客户端。然后我应该输入更多的行,直到我输入单个字母,如“Q” – BeyondDays
当您尝试两行时会发生什么情况?是键入内容,按回车,请求运行,服务器返回适当的响应,然后客户端程序结束?你能指出你认为应该导致程序要求第二行的代码行吗? –