简体   繁体   English

虽然真正的循环被Java中的try / catch破坏了

[英]While true loop being broken by a try/catch in Java

I am a beginner to networking and am working on implementing it in a game i have, it is mostly working but in the client my loop receiving packets is stopping ant a try catch. 我是网络的初学者,正在尝试在我拥有的游戏中实现它,该游戏大部分都可以运行,但是在客户端,我的接收数据包的循环正在停止尝试捕获。 here is the code: 这是代码:

public void run() {
    while(true){
        byte[] data = new byte[1024];
        DatagramPacket packet = new DatagramPacket(data, data.length);
        try {
            socket.receive(packet);
        }catch(IOException e){
            e.printStackTrace();
        }

        this.parsePacket(packet.getData(), packet.getAddress(), packet.getPort());
    }
}

After debugging i have found that the loop is not getting past the try catch statement.(but the try is being activated once seemingly with no error). 调试后,我发现循环没有越过try catch语句。(但是try似乎被激活一次,没有错误)。

First of all your loop condition that you provided is not a variable , You have directly used a boolean valued true . 首先,您提供的循环条件不是variable ,您直接使用了布尔值true It might a situation of infinite-loop . 可能是infinite-loop

 while(true){..... // yourCode ....}

Your loop will continue even if socket.receive(packet); 即使socket.receive(packet);您的循环也将继续socket.receive(packet); line raise an IOException because this exception is handled by the catch() block. 引发IOException因为此异常由catch()块处理。

it means it will execute till any exception raised at this line 这意味着它将一直执行到此行引发任何异常为止

this.parsePacket(packet.getData(), packet.getAddress(), packet.getPort());

Now, Your problem is this method public void receive(DatagramPacket p) actually method will work till datagram is received but your loop will continue till exception is raised till this.parsePacket() method raised an Exception. 现在,您的问题是此方法public void receive(DatagramPacket p)实际上方法将一直工作到收到datagram为止,但是您的循环将继续进行,直到引发异常,直到this.parsePacket()方法引发Exception。

So, please change your conditional statement such that if datagram is no more present then loop must be terminated. 因此,请更改您的条件语句,以便如果不再存在datagram ,则必须终止循环。

As per java-docs for the method public void receive(DatagramPacket p) 根据方法的java-docs public void receive(DatagramPacket p)

This method blocks until a datagram is received. 该方法将阻塞,直到接收到数据报为止。

The thread executing the flow gets blocked [waits until the datagram is received]. 执行流的线程被阻塞[等待直到接收到数据报]。 Your loop is not broken its just in halt state. 您的循环并没有因此而处于中断状态。 Loop will be broken in case of some exception occurs which is not caught and handled in side the body of the loop. 如果发生某些异常,并且该异常未在循环主体中捕获和处理,则循环将被破坏。

Also there is little suggestion I would like you to implement in your code. 另外,我也很少建议您在代码中实现。 You should not run an infinite loop. 您不应该运行无限循环。 Instead of using true you can try using Thread.currentThread().isInterrupted() and when your game ends you can call interrupt() on the Thread responsible for executing the code flow of the loop. 除了使用true还可以尝试使用Thread.currentThread().isInterrupted()并且当游戏结束时,您可以在负责执行循环代码流的Thread上调用interrupt() Also if in case of IOException you wish to retry (may be in retry resources are in order and you do not get the exception) then putting catch inside the loop is OK else you can move the catch outside the loop, it depends on your need and flow. 同样,如果在IOException的情况下您希望重试(可能是重试中的资源是有序的,但是您没有得到异常),那么将catch放入循环中就可以了,否则您可以将catch移到循环外,这取决于您的需要和流动。 You can probably put a counter for number of retries. 您可以为重试次数设置一个计数器。

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

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