2017-03-29 86 views
0

我現在遇到了這個問題。 對於這個問題,我必須:通過套接字壓縮和解壓縮數據

客戶端:壓縮從控制檯的各輸入線,將其發送到服務器和從服務器

服務器端解壓縮每個消息:解壓縮從客戶端的數據,改變低 - 將大寫字母壓縮併發送回客戶端

我能做的最好的事情就是隻用一行就可以完成上述任何操作。

客戶端:

/* 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(); 
} 

}

+0

問題是什麼?你期望發生什麼?會發生什麼呢? – cubrr

+0

那麼,如果我在控制檯上輸入一行代碼,我只能讓它工作。我應該做的是輸入一行,壓縮它,發送到服務器,服務器解壓縮它和大寫字母,壓縮它,發送回客戶端。然後我應該輸入更多的行,直到我輸入單個字母,如「Q」 – BeyondDays

+0

當您嘗試兩行時會發生什麼情況?是鍵入內容,按回車,請求運行,服務器返回適當的響應,然後客戶端程序結束?你能指出你認爲應該導致程序要求第二行的代碼行嗎? –

回答

0

您不能交互使用這些流。在每次寫入之後,或者在每次讀取之前,您都必須撥打finish(),這意味着您只能進行一次寫入。它們設計用於大型單向流,而不是交互式請求/響應會話。

在任何情況下,壓縮單行都沒有好處。你需要大量的壓縮數據才能開始工作。

NB compress.write(line.getBytes(), 0, line.length())無效。它假定String中的字符數與轉換時的字節數相同,但並非總是如此。它應該是compress.write(line.getBytes(), 0, line.getBytes().length()),或者更簡單的compress.write(line.getBytes())

+0

因此,在這種情況下,是最好把輸入行放入一個文本文件,壓縮並將其發送到服務器? DeflaterOutputStream就好了,對吧? – BeyondDays

+0

@BeyondDays'DeflaterOutputStream'會是更好的選擇。寫一個文本文件肯定是徒勞的;編寫一個壓縮文件然後發送它會增加很多不需要的延遲。 – EJP