user3710112
user3710112

Reputation: 13

Java server socket program after one day it stops listening

I have a server and that machine i run the server socket code. I have used thread pool to maintain the thread. The problem is, when i start the server socket it's listening fine. But the next day i see the console that is paused at some level and no listening at any TCP packets. There is no exception generated. Is there any limit of listening packets on server in java? How can i resolve this problem?

Server class

public final class Server {

    private final ServerSocket serverSocket;
    private final ExecutorService pool;



    public Server(int port, int poolSize)
            throws IOException {
        serverSocket = new ServerSocket(port);
        pool = Executors.newFixedThreadPool(poolSize);


        System.out.println("Server running...");
        runServerEngine();
    }

    public void runServerEngine() { // run the service
        try {
            while (true) {
                System.out.println("user come ");
                pool.execute(new Handler(serverSocket.accept()));
            }
        } catch (IOException ex) {
            System.out.println("Error " + ex);


            //pool.shutdown();
        }
    }

    public static void main(String[] args) throws IOException {
        Server server = new Server(1256, 20);//port number and pooling size
    }
}

Handler class

class Handler implements Runnable {

    private Socket socket = null;
    //private static Location location = new Location();


    Handler(Socket socket) {
        this.socket = socket;

    }

    private Handler() {
    }
    String recievedPacket = null;

    @Override
    public void run() {
        // read and service request on socket
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));

            while ((recievedPacket = in.readLine()) != null) {
                //recievedPacket = in.readLine();
                System.out.println("Recieved packate " + recievedPacket);

                if (recievedPacket != null) {
                    try {
                        System.out.println("Saving function " + recievedPacket);


                    } catch (Exception ex) {
                         System.out.println(ex);
                    }

                }

            }

        } catch (IOException ex) {
           System.out.println(ex);
        }
    }
}

Upvotes: 1

Views: 841

Answers (1)

Jan Zyka
Jan Zyka

Reputation: 17898

Try closing the in and socket in finally block of Handler to make sure your system is not out of resources.

@Override
public void run() {
    // read and service request on socket
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));

        while ((recievedPacket = in.readLine()) != null) {
            //recievedPacket = in.readLine();
            System.out.println("Recieved packate " + recievedPacket);

            if (recievedPacket != null) {
                try {
                    System.out.println("Saving function " + recievedPacket);


                } catch (Exception ex) {
                     System.out.println(ex);
                }

            }

        }

    } catch (IOException ex) {
       System.out.println(ex);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                // No-OP
            }

        }

        if (socket != null) {
            try {
                scoket.close();
            } catch (IOException ex) {
                // No-OP
            }
        }
    }
}

If you are Java 7+ you may want to have a look on try with resources.

Upvotes: 1

Related Questions