简体   繁体   English

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

[英]java opening a socket on the client side

i can't figure out why my client socket keeps falling in an IOException. 我不知道为什么我的客户端套接字不断陷入IOException。 When it falls in the IOException i recreate my socket and so it will work again untill it falls again into the Exception and so on ... 当它落入IOException时,我会重新创建我的套接字,因此它将再次工作,直到它再次落入Exception之类,依此类推...

the code should always read out an ip controller from my electrical installation. 该代码应始终从我的电气安装中读出IP控制器。 I cant change anything on the server side. 我无法在服务器端进行任何更改。 I connect to it and when something happens it post it to the port and i want to be able to read it. 我连接到它,并且在发生某种情况时将其发布到端口,并且我希望能够读取它。

this is the opening socket code 这是开始的套接字代码

    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");
    }

and here i read out the data 在这里我读出了数据

        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();
            }
        }       
    }

it workes but far from good coding. 它的工作原理,但远非良好的编码。 any help on how to solve this would be appreciated. 任何有关如何解决此问题的帮助将不胜感激。

cleaned the above code for anyone who's interested 为有兴趣的人清除了上面的代码

opening a socket 打开插座

    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");
    }
}

socketlistener in new thread public class KnxSocketListener 新线程公共类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();
        }       
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM