开发者

problem in understanding java sockets

开发者 https://www.devze.com 2023-03-10 18:15 出处:网络
I have an app in java which is playing the rolle of a server .For limiting the number of incoming connections I\'m using a ThreadPool server.

I have an app in java which is playing the rolle of a server .For limiting the number of incoming connections I'm using a ThreadPool server.

But I have a few problems understanding a part of the code:

Here is y code:


 protected ExecutorService threadPool =
        Executors.newFixedThreadPool(5);开发者_Python百科


public ThreadPooledServer(BlockingQueue queue,int port) {
    this.serverPort = port;
    this.queue=queue;

}

while (!isStopped()) {

        Socket clientSocket = null;
        try {
            System.out.println("Serverul asteapta clienti spre conectare la port" +serverPort);
            clientSocket = serverSocket.accept();
            clientconnection++;
            System.out.println("Serverul a acceptat clientul cu numarul:"
                    + clientconnection);


        } catch (IOException e) {
            if (isStopped()) {
                System.out.println("Server Stopped.");
                return;
            }
            throw new RuntimeException("Error accepting client connection",
                    e);


        }

    WorkerRunnable workerRunnable = new WorkerRunnable(queue,clientSocket);

    this.threadPool.execute(workerRunnable);

    }

    this.threadPool.shutdown();

    System.out.println("Server Stopped.");

}



private synchronized boolean isStopped() {

    return this.isStopped;

}




public synchronized void stop() {

    this.isStopped = true;

    try {

        this.serverSocket.close();

    }


    catch (IOException e) {

        throw new RuntimeException("Error closing server", e);

    }

}

private void openServerSocket() {

    try {

        InetSocketAddress serverAddr = new InetSocketAddress(SERVERIP,
                serverPort);

        serverSocket = new ServerSocket();


        serverSocket.bind(serverAddr);

    } catch (IOException e) {

        throw new RuntimeException("Cannot open port", e);

    }

}

WHAT I don't understand:

I'm using a ThreadPooledServer which accepts for 5 incoming connections....

The connection with the clients is done in a while() loop.

while (!isStopped()) {

}

isStopped is a boolean variable returned byt this function:

private synchronized boolean isStopped() {

    return this.isStopped;

}

which I call as a condition for starting the loop.

This boolean variable is initially set to false.....and is set back to true in the here:

public synchronized void stop() {

        this.isStopped = true;

}

When is setup back to true my while() loop ends and then I close up all the workers of my thread pool.

this.threadPool.shutdown();

The problem is that I never call for this function " stop() "

Question: Is the function called automatically when I close my server?????...or I should call for it somewhere????


You need to call it somewhere in your code to stop your server and close those connections. If you don't the system will eventually reclaim its resources as the server will be shutting down.

You should be able to register a shutdown hook in the JVM (which can call stop()) to help with reclaiming those yourself... Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消