简体   繁体   English

如何在 Java 应用程序中使用 SSLSockets 而不是标准 Sockets

[英]How do I use SSLSockets instead of standard Sockets with Java apps

I'm trying to use ssl sockets in a working java networking program.我正在尝试在工作的 java 网络程序中使用 ssl sockets 。

I replaced two lines of code我替换了两行代码

        //Socket socket = new Socket(ipAddress, port);
        SSLSocket socket = (SSLSocket)SSLSocketFactory.getDefault().createSocket(ipAddress, port);

and

        //serverSocket = new ServerSocket();
        serverSocket = (SSLServerSocket)SSLServerSocketFactory.getDefault().createServerSocket();

        serverSocket.setReuseAddress(true);
        serverSocket.bind(new InetSocketAddress(port));
        while (true) {
            Socket clientSocket = serverSocket.accept();
            ......

I get an exception: "connection refused: Connect"我得到一个例外:“连接被拒绝:连接”

The program was working with the commented code.该程序正在使用注释代码。 And now it doesn't.而现在它没有。 What do I need to do to use SSLSockets instead of standard Sockets?我需要做什么才能使用 SSLSockets 而不是标准的 Sockets?

If relevant, the server is running on localhost == windows vista.如果相关,服务器在 localhost == windows vista 上运行。

'Connection refused' only has two meanings whether plaintext or SSL. “连接被拒绝”只有两种含义,无论是明文还是 SSL。 There is nothing listening at the ip:port you tried to connect to, or an intermediate firewall explicitly blocked the connect by sending an RST. ip:您尝试连接的端口没有任何监听,或者中间防火墙通过发送 RST 显式阻止了连接。

Your second line of code creates an SSLServerSocket on an arbitrary port, because you left out the 'port' parameter.您的第二行代码在任意端口上创建了 SSLServerSocket,因为您省略了“端口”参数。 That's almost certainly the problem.这几乎肯定是问题所在。

Bruno gave me the answer in the comments: I haven't configured any keystore.布鲁诺在评论中给了我答案:我没有配置任何密钥库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM