java在客户端打开一个套接字

时间:2016-12-07 11:52:12

标签: java sockets listener

我无法弄清楚为什么我的客户端套接字在IOException中不断下降。 当它落入IOException时,我重新创建我的套接字,因此它将再次起作用,直到它再次落入Exception等等......

代码应始终从我的电气安装中读出ip控制器。我无法改变服务器端的任何内容。我连接到它,当有什么事情发生时,它将它发布到端口,我希望能够阅读它。

这是开放套接字代码

    public KnxController(){
    try{
        System.out.println("Server started");
        clientSocket = new Socket(IP, PORT);
        outToServer = new DataOutputStream(clientSocket.getOutputStream());
    } catch (UnknownHostException e) {
        System.out.println("UnknownHostException: kan knx niet vinden");
    } catch (IOException e) {
        System.out.println("IOException knxcontroller: kan geen data sturen");
    }

在这里我读出了数据

        while(true){
        try {
            DataInputStream din=new DataInputStream(knxC.clientSocket.getInputStream()); 
            BufferedReader br = new BufferedReader(new InputStreamReader(din));
            String[] str=br.readLine().split("");  
            System.out.println(Arrays.toString(str));

        } catch (IOException e) {
            try {
                knxC.clientSocket = new Socket(knxC.IP, knxC.PORT);
            } catch (UnknownHostException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } catch(NullPointerException nex){
            try {
                knxC.clientSocket = new Socket(knxC.IP, knxC.PORT);
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }       
    }

它起作用但远没有良好的编码。 如何解决这个问题的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

为所有感兴趣的人清理上述代码

打开一个套接字

    public KnxController(){
    do{
        setClientSocket();

        try {
            outToServer = new DataOutputStream(clientSocket.getOutputStream());
        } catch (IOException e) {
            log.error("IOException knxcontroller: kan geen data out stream starten naar knx");
        } catch (NullPointerException nEx){
            log.error("NullPointerException knxcontroller: geen knx host");
        }
    }while(clientSocket == null);

    KnxSocketListener knxL = new KnxSocketListener("KnxSocketListener", this);
    knxL.start();
}

    public void setClientSocket() {
    try {
        this.clientSocket = new Socket(IP, PORT);
    } catch (UnknownHostException e) {
        log.error("UnknownHostException: kan geen socket opbouwen met knx host");
    } catch (IOException e) {
        log.error("IOException: kan geen socket opbouwen met knx host");
    }
}

新线程公共类KnxSocketListener

中的socketlistener
public KnxSocketListener(String name, KnxController knxC) {
      threadName = name;
      System.out.println("Creating " +  threadName );
      this.knxC = knxC;
}

public void start () {
      System.out.println("Starting " +  threadName );
      if (t == null) {
         t = new Thread (this, threadName);
         t.start ();
      }
}   

public void run()
{
    while(true){
        try {
            DataInputStream din = new DataInputStream(knxC.getClientSocket().getInputStream()); 
            BufferedReader br = new BufferedReader(new InputStreamReader(din));
            String[] str=br.readLine().split("");  
            System.out.println(Arrays.toString(str));
        } catch (IOException e) {
            knxC.setClientSocket();
        } catch(NullPointerException nex){
            knxC.setClientSocket();
        }       
    }
}