2012-12-24 42 views
1

我寫了一個使用SSL套接字的客戶機/服務器套接字程序。服務器和客戶端之間的連接已建立,但我無法在數據流上寫入數據。以下是爲服務器和客戶端無法在SSLSocket上寫入數據?

服務器(這將創建SSLServerSocket併爲每個客戶端創建新的線程)的代碼段

System.setProperty("javax.net.ssl.keyStore", "D:\\client\\server_keystore.jks"); 
System.setProperty("javax.net.ssl.keyStorePassword", "helloworld"); 
boolean listening = true; 
SSLServerSocketFactory sslserversocketfactory = null; 
SSLServerSocket sslserversocket = null; 

try { 

    sslserversocketfactory = 
     (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); 
    sslserversocket = 
     (SSLServerSocket) sslserversocketfactory.createServerSocket(Integer.parseInt(args[0])); 

} catch (IOException e) { 

    System.out.println("Input/output error occured :" + e.getMessage()); 
    System.exit(-1); 
} 

while (listening) { 
    try { 
     Thread t = new Thread(new DeprovisionServerThread(sslserversocket.accept())); 
     if (t != null) { 
      System.out.println("New thread created: " + t.getName()); 
     } 

     t.start(); 
    } catch (IOException ex) { 
     Logger.getLogger(DeprovisionServer.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
try { 
    sslserversocket.close(); 
} catch (IOException ex) { 
    Logger.getLogger(DeprovisionServer.class.getName()).log(Level.SEVERE, null, ex); 
} 

服務器(線程處理代碼)

DeprovisionServerThread(Socket socket) { 
    // throw new UnsupportedOperationException("Not yet implemented"); 

    sslsocket = (SSLSocket) socket; 


} 

@Override 
public void run() { 
    //throw new UnsupportedOperationException("Not supported yet."); 
    System.out.println("New client socket connection accpeted from: " + sslsocket.getInetAddress() + " : " + sslsocket.getLocalPort()); 
    try { 
     //PrintWriter out = new PrintWriter(sslsocket.getOutputStream(), true); 

     BufferedWriter w = new BufferedWriter(new OutputStreamWriter(sslsocket.getOutputStream())); 
     BufferedReader in = new BufferedReader(
       new InputStreamReader(
       sslsocket.getInputStream())); 


     String inputLine, outputLine; 


     // out.println(outputLine); 

     while ((inputLine = in.readLine()) != null) { 
     // System.out.println(inputLine); 
     if(inputLine!= null)System.out.println("command received" + inputLine); 
     w.write("success"); 

     } 
     // out.close(); 
     in.close(); 
     sslsocket.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 

客戶端代碼:

System.setProperty("javax.net.ssl.trustStore", "D:\\server\\server_keystore.jks"); 
     System.setProperty("javax.net.ssl.trustStorePassword", "helloworld"); 
     BufferedReader in = new BufferedReader(
       new InputStreamReader(System.in)); 
     PrintStream out = System.out; 
     SSLSocketFactory f = 
       (SSLSocketFactory) SSLSocketFactory.getDefault(); 
     try { 
      SSLSocket c = 
        (SSLSocket) f.createSocket("localhost", 4444); 
      // c.s 
      c.addHandshakeCompletedListener(new MyListener()); 

      c.startHandshake(); 
      Thread.sleep(5000l); 
      printSocketInfo(c); 
      BufferedWriter w = new BufferedWriter(
        new OutputStreamWriter(c.getOutputStream())); 
      BufferedReader r = new BufferedReader(
        new InputStreamReader(c.getInputStream())); 

      w.write("deprovision command",0, 10); 
      System.out.println("send.."); 
      w.flush(); 
      String m ; 
      while ((m = r.readLine()) != null) { 
      System.out.println("status received: " + m); 


      } 
      System.out.println("after while"); 
      w.close(); 
      r.close(); 
      c.close(); 
     } catch (IOException e) { 
      System.err.println(e.toString()); 
     } 
    } 

    private static void printSocketInfo(SSLSocket s) { 
     System.out.println("Socket class: " + s.getClass()); 
     System.out.println(" Remote address = " 
       + s.getInetAddress().toString()); 
     System.out.println(" Remote port = " + s.getPort()); 
     System.out.println(" Local socket address = " 
       + s.getLocalSocketAddress().toString()); 
     System.out.println(" Local address = " 
       + s.getLocalAddress().toString()); 
     System.out.println(" Local port = " + s.getLocalPort()); 
     System.out.println(" Need client authentication = " 
       + s.getNeedClientAuth()); 
     SSLSession ss = s.getSession(); 
     System.out.println(" Cipher suite = " + ss.getCipherSuite()); 
     System.out.println(" Protocol = " + ss.getProtocol()); 

    } 
} 

即時測試本地系統上的此代碼。我還使用監聽器來檢查握手過程中是否沒有問題。當一個新的客戶端來到時,服務器接受來自客戶端的連接,並且打印所有在printSocketInfo()方法中打印的信息。握手偵聽器也會收到通知。但是當我從客戶端套接字向服務器寫入數據時沒有任何事件發生,沒有任何例外。請幫忙 。在此先感謝

+0

可以請您更新服務器處理代碼,因爲它是現在複製主服務器例行 – hoaz

+0

嗨hoaz。請找到更新後的代碼 – pyus13

+0

'Thread t'不可能在你測試的時候爲null。 – EJP

回答

4

從「deprovision命令」字符串中只寫入10個第一個字符,並且服務器期望以新行字符結尾的字符串。客戶端的代碼更改爲以下:

w.write("deprovision command",0, 10); 
w.newLine(); 
System.out.println("send.."); 
w.flush(); 
+0

非常感謝。 – pyus13