0

我正在努力查看这里出了什么问题(课程的一部分):

public class Network implements Runnable {    
    private static Network cachedInstance;

    private static boolean connected;

    //private Socket clientSocket;
    private SSLSocket clientSocket;
    private PrintWriter out;
    private BufferedReader in;

    private Map<String, SyncObject> waitForServer;

    private Network(final String hostname, final int port) {
        try {
            Socket baseSocket = new Socket();
            baseSocket.connect(new InetSocketAddress(hostname, port), Config.TIMEOUT);
            clientSocket = (SSLSocket)SSLSocketFactory.createSocket(baseSocket, hostname, port, true);
            clientSocket.startHandshake();
            out = new PrintWriter(clientSocket.getOutputStream(), true);     
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            connected = true;
        } catch (IOException ex) { 
            connected = false;
        }

        waitForServer = new HashMap<>();
    }

我收到一个错误:

C:\Users\Frank\Dropbox\NetbeansProjects\TradingCardGame\src\network\Network.java:47: error: non-static method createSocket(Socket,String,int,boolean) cannot be referenced from a static context
        clientSocket = (SSLSocket)SSLSocketFactory.createSocket(baseSocket, hostname, port, true);

我什至如何处于静态环境中?

问候。

4

1 回答 1

2

SSLSocketFactory.createSocket一个实例方法。这意味着SSLSocketFactory应该先创建一个类的实例,然后才能对其调用createSocket方法。

你可以试试这个

SocketFactory sslSocketFactory = SSLSocketFactory.getDefault();
clientSocket = (SSLSocket) sslSocketFactory.createSocket(baseSocket, hostname, port, true);
于 2013-05-19T20:21:12.933 回答