执行isConnected()时Socket返回null

时间:2016-12-09 18:19:42

标签: java sockets

我正在尝试连接到套接字,然后检查它是否已连接。我的问题是我的socket是连接的,但是当我询问它是否时,它返回null。

我如何获得我的联系:

package main;

import java.io.IOException;
import java.net.Socket;

public class getIrcConnection {

    String Server = "tmi.twitch.tv";
    int Port = 80;

    declars declars = new declars();

    public getIrcConnection() throws IOException{

    this.declars.socket = new Socket(Server, Port);

    }

}

我的套接字存储位置:

package main;

import java.net.Socket;

public class declars {

    Socket socket;

}

我如何检查连接:

package main;


public class checkIrcConnection {

    declars declars = new declars();

    public checkIrcConnection() {

        if (this.declars.socket.isConnected()) {           
        System.out.println("Connected");     
        }

      }

}

所有这些都是在主要方法中执行的:

package main;

public class Main {

    public static void main(String[] args) throws Exception {

        getIrcConnection gic = new getIrcConnection();

        checkIrcConnection cic = new checkIrcConnection();

    }

}

2 个答案:

答案 0 :(得分:1)

您有两个%1./konvertiermich/andere/Paper-Leben-Modeliranje_delovnih_procesov/obdelava_narocil.epml.pl v 1_i_2 startevent v 1_i_17 endevent v 1_i_23 endevent v 1_i_26 endevent v 1_i_4 task v 1_i_5 task v 1_i_9 task v 1_i_11 task v 1_i_20 task v 1_i_21 task v 1_i_25 task v 1_i_6 xor v 1_i_10 xor v 1_i_13 xor v 1_i_14 xor v 1_i_15 xor v 1_i_19 xor v 1_i_22 xor d 1_i_2 1_i_4 arc d 1_i_5 1_i_6 arc d 1_i_9 1_i_10 arc d 1_i_10 1_i_14 arc d 1_i_10 1_i_15 arc d 1_i_11 1_i_13 arc d 1_i_13 1_i_14 arc d 1_i_13 1_i_15 arc d 1_i_20 1_i_17 arc d 1_i_19 1_i_21 arc d 1_i_21 1_i_22 arc d 1_i_22 1_i_23 arc d 1_i_25 1_i_26 arc d 1_i_4 1_i_5 arc d 1_i_6 1_i_9 arc d 1_i_6 1_i_19 arc d 1_i_10 1_i_11 arc d 1_i_15 1_i_20 arc d 1_i_14 1_i_19 arc d 1_i_22 1_i_25 arc %2./konvertiermich/andere/Web-Wikipedia.cz/wikipedia.cz.epml.pl v 2_i_6 startevent v 2_i_7 endevent v 2_i_1 task v 2_i_2 task v 2_i_3 task v 2_i_4 task v 2_i_5 task v 2_i_15 and v 2_i_17 and v 2_i_14 xor v 2_i_16 xor v 2_i_18 xor v 2_i_19 xor d 2_i_1 2_i_14 arc d 2_i_3 2_i_18 arc d 2_i_6 2_i_3 arc d 2_i_14 2_i_7 arc d 2_i_15 2_i_5 arc d 2_i_16 2_i_1 arc d 2_i_17 2_i_4 arc d 2_i_17 2_i_2 arc d 2_i_19 2_i_17 arc d 2_i_2 2_i_15 arc d 2_i_4 2_i_15 arc d 2_i_18 2_i_19 arc d 2_i_5 2_i_16 arc d 2_i_14 2_i_19 arc d 2_i_18 2_i_16 arc %3./konvertiermich/deutsch/BA-Blau-Customer_Relationship_Management_gestützte_Prozesse_am_Beispiel_des_Unternehmens_Alere/39-Angebotsprozess.pl v 3_i_1 startevent v 3_i_19 endevent v 3_i_2 task v 3_i_6 task v 3_i_7 task v 3_i_10 task v 3_i_13 task v 3_i_15 task v 3_i_18 task v 3_i_3 xor v 3_i_8 xor v 3_i_12 xor v 3_i_17 xor d 3_i_1 3_i_2 arc d 3_i_2 3_i_3 arc d 3_i_6 3_i_8 arc d 3_i_7 3_i_8 arc d 3_i_12 3_i_13 arc d 3_i_17 3_i_18 arc d 3_i_18 3_i_19 arc d 3_i_12 3_i_17 arc d 3_i_3 3_i_6 arc d 3_i_3 3_i_7 arc d 3_i_8 3_i_10 arc d 3_i_10 3_i_12 arc d 3_i_13 3_i_15 arc d 3_i_15 3_i_17 arc 个对象。一个是您设置declars而另一个是gic.declars。即你正在使用你未设置的那个。

我建议您逐步调试调试器中的代码,因为您有一个简单的混淆,这应该从调试器中显而易见。

答案 1 :(得分:0)

首先以面向对象的方式组织代码。将类视为对象,这些对象必须具有公共属性和常用操作。然后将所有功能分发给对象。如果你这样做,调试和理解错误就会容易得多。

如果你想对URL做一些http请求,并处理返回的结果,那么使用一些库会很好。例如,您可以使用unirest

或者如果您想在java中使用套接字编程,可以参考java tutorial about sockets