Java SSL -OpenSSL- Snafu

时间:2012-09-10 14:27:10

标签: java sockets ssl applet

下面是我发现的关于'网络的一些代码,我无法使其正常工作。据我所知它应该工作。遗憾的是,当我在OpenSSL工具包中使用s_server时,它没有注册连接。服务器是用Cpp编写的,并且工作正常(至少我有那么多)。任何人都可以对我做出适当的更正,我们将不胜感激。

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;

import java.applet.*;
import java.awt.*;


public class jclientssl extends Applet {

    public static void main() {

        try {
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9999);

            InputStream inputstream = System.in;
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

            OutputStream outputstream = sslsocket.getOutputStream();
            OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
            BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);

            String string = null;
            while ((string = bufferedreader.readLine()) != null) {
                bufferedwriter.write(string + '\n');
                bufferedwriter.flush();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public void paint(Graphics g) {

        g.drawString("Welcome to Java!!", 50, 60 );

    }
}

1 个答案:

答案 0 :(得分:2)

如果您将此代码作为Applet运行,则main method is irrelevant,因此您的套接字代码未被执行。 (并且,如果要在独立应用程序中使用该main方法,则需要public static void main(String[]))。