简体   繁体   中英

How to run a Server without using a while loop?

I'm trying to make a server program using the DatagramSocket and DatagramPacket classes, but my current code uses an ugly while loop which also freezes my program while the server runs, but the server runs fine with no problems. Anyway the code is below. Is there anyway I can use something different than a while loop or prevent the while loop from preventing any other code in the program to execute?

protected void run() {
    try {
        socket = new DatagramSocket(port);
        socket.setBroadcast(true);
    } catch (Exception e) {
        e.printStackTrace();
        stopServer(); //stop the server
        return; 
    }
    while(isRunning()){ //hate this loop
        try {
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket(buf, 256);
            socket.receive(packet);
            DatagramPacket serverPacket;
            byte[] serverBuf;
            byte hb = 0;
            byte lb = packet.getData()[0];
            int e = ((int)hb<<8)|((int)lb&0xFF); //translate byte to int
            switch(e) {
                case 2:
                    //do something
                    break;
                case 5:
                   //do something
                   break;
                case 7:
                    //do something
                    break;
                default:
                    //default stuff
                    break;
            }
        } catch (Exception e) {
            System.out.println("Fatal Server Error:");
            e.printStackTrace(); 
            stopServer(); //stop the server
            return; //stop the method
        }
    }
}

You should put this in a new thread. The process of receiving a packet involves synchronous IO, so it will always block whichever thread it's running in.

To clarify, it's not the while loop itself that makes the rest of the program wait. It's the socket.receive() call.

I doubt that you can get rid of the loop because you, logically, intend that your server "serves" multiple requests. Every request will be served in one iteration from the server loop.

Now, it is customary that your server will only care about receiving the requests and will dispatch them to a thread pool to be processed.

As such, you do not make your server thread worry about the processing of a given request. Basically because if your server is busy processing the request it cannot attend any other ones arriving to its port during that time.

The recommendation would be, receive the packets, and immediatelly pass the received data to another thread for processing.

This, evidently, will not imply the removal of the loop in question, which as I mentioned, is not possible unless you intend to serve only one request from your clients (a rather unlikely scenario). This would guarantee that the requests can be proccessed independently and their processing will not delay attending other requests arriving at the server port.

I would recommend using Apache Mina to manage your flow and network communication.

It was designed for this particularly and provides an implementation of the Reactor design pattern which might be a better design choice for your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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