2013-06-19 79 views
1

我正在使用安全連接(ssl)的java(服務器/客戶端)中創建應用程序。Java ssl服務器/客戶端應用程序傳遞字節[]

主要類有插座和流初始化:

服務器主類:

public static void main(String[] arstring) { 
      DataOutputStream out = null; 
    DataInputStream in = null; 
    SSLServerSocket sslserversocket = null; 
    SSLSocket sslsocket = null; 
... 

客戶端主類:

public static void main(String[] args) throws IOException { 
    BufferedReader console; 
    DataInputStream in = null; 
    DataOutputStream out = null; 
    SSLSocket sslsocket = null; 
    ... 

我已經創建了相應的插座和流在每個類中:

服務器套接字和流:

SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory 
        .getDefault(); 
      sslserversocket = (SSLServerSocket) sslserversocketfactory 
        .createServerSocket(port); 
      System.out.println("Server: listening on port: " + port + "\n"); 
      System.out.println("Waiting for connection...." + "\n"); 
      sslsocket = (SSLSocket) sslserversocket.accept(); 
      connected = true; 
      System.out.println("Connection accepted \n"); 

      InputStream inputstreamconsola = System.in; 
      InputStreamReader inputstreamreaderconsola = new InputStreamReader(
        inputstreamconsola); 
      BufferedReader bufferedreaderconsola = new BufferedReader(
        inputstreamreaderconsola); 

      in = new DataInputStream(sslsocket.getInputStream()); 
      out = new DataOutputStream(sslsocket.getOutputStream()); 

客戶端套接字和流:

SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); 
      sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9999); 
      in = new DataInputStream(sslsocket.getInputStream()); 
      out = new DataOutputStream(sslsocket.getOutputStream()); 

我遇到的問題是,當我想通過一個byte []從服務器到客戶端,字節[]已在正確的值服務器,但它到達客戶端的空值。 注:當我使用的readUTF()和writeUTF()來傳遞字符串,發送的值被正確接收

「錯誤」 在這種情況下發生:

服務器:

 byte[] nonceBytes = new byte[(int) 8]; 
    nonce = System.currentTimeMillis(); 
    nonceBytes = longToByteArray(nonce); 

    System.out.print("\nNonce gerado: "); 
     for (int i = 0; i < 8; i++) 
     System.out.print(getHexString(nonceBytes[i])); 
    System.out.print("\n"); 
      out.writeUTF("pass#"); 
    out.write(nonceBytes, (int) 0, nonceBytes.length); VALUE HERE IS CORRECT 

客戶端:

   byte[] nonceBytes = new byte[(int) 8]; 
       int nbytes = 0; 

       // read the nonce sent by server 
       try { 
        nbytes = in.read(nonceBytes); !!nonceBytes gets the null value instead of the value passed by the server!! 
       } catch (IOException e) { 
        System.err.println("Read: " + e.getMessage()); 
        readInput = false; 
        break; 
       } 

我想明白,爲什麼我不能用方法out.write發送字節[]()的原因,並通過該方法得到它在客戶端in.read()。

回答

1

你忽略了read().返回的結果你不能假定它填充緩衝區。在這種情況下,您應該使用DataInputStream.readFully()

請注意,這個問題與SSL無關。

+0

但read()返回的結果在客戶端和使用後保存爲nbytes。我不明白的是nonceBytes的原因從服務器中的write()獲得null。 –

+0

你的建議工作EJP,但我想明白爲什麼read()不起作用.. 無論如何非常感謝你 最好的問候 –

+0

它確實工作。它只是不會做你認爲的事情。這不是一回事。看到Javadoc。它沒有說任何它填補緩衝區。相反,它需要一定的時間來解釋它可能不會。 – EJP

相關問題