无法在SSLSocket上写入数据?

时间:2012-12-24 10:06:52

标签: java jsse

我编写了一个使用SSL套接字的客户端/服务器套接字程序。服务器和客户端之间建立连接,但我无法在流上写入数据。以下是服务器和客户端的代码

SERVER(这会创建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()方法中打印的所有信息。握手监听器也会得到通知。但是当我从客户端套接字向服务器写入数据时没有任何事情发生,没有例外。请帮忙 。提前谢谢

1 个答案:

答案 0 :(得分:4)

您只能从“deprovision命令”字符串中编写10个第一个字符,而服务器需要以新行字符结尾的字符串。将您的客户端代码更改为以下内容:

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