2012-12-24 28 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