1

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

服务器(这将创建 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() 方法中打印的信息。握手侦听器也收到通知。但是当我将数据从客户端套接字写入服务器时,什么也没发生,也不例外。请帮忙 。提前致谢

4

1 回答 1

4

您从“deprovision command”字符串中仅写入 10 个前字符,并且服务器需要以换行符结尾的字符串。将您的客户端代码更改为以下内容:

w.write("deprovision command",0, 10);
w.newLine();
System.out.println("send..");
w.flush();
于 2012-12-24T10:50:52.120 回答