我現在遇到了這個問題。 對於這個問題,我必須:通過套接字壓縮和解壓縮數據
客戶端:壓縮從控制檯的各輸入線,將其發送到服務器和從服務器
服務器端解壓縮每個消息:解壓縮從客戶端的數據,改變低 - 將大寫字母壓縮併發送回客戶端
我能做的最好的事情就是隻用一行就可以完成上述任何操作。
客戶端:
/* 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
當您嘗試兩行時會發生什麼情況?是鍵入內容,按回車,請求運行,服務器返回適當的響應,然後客戶端程序結束?你能指出你認爲應該導致程序要求第二行的代碼行嗎? –