1

下面是我发现的一些关于“网络”的代码,但我无法让它工作。据我所知,它应该工作。遗憾的是,当我在 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 );

    }
}
4

1 回答 1

2

如果您将此代码作为 Applet 运行,则main 方法无关紧要,因此您的套接字代码不会被执行。(并且,如果您想在独立应用程序中使用该 main 方法,那么它必须是public static void main(String[]))。

于 2012-09-10T14:51:04.480 回答