Java服务器套接字程序在一天后停止监听

时间:2014-06-05 09:04:25

标签: java multithreading sockets

我有一台服务器和那台机器我运行服务器套接字代码。 我已经使用线程池来维护线程。 问题是,当我启动服务器套接字时,它听起来很好。 但是第二天我看到控制台在某个级别暂停,并且没有监听任何TCP数据包。 没有生成异常。 java中的服务器上是否有监听数据包的限制? 我该如何解决这个问题?

服务器类

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
    }
}

处理程序类

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

1 个答案:

答案 0 :(得分:1)

尝试关闭in的finally块中的socketHandler,以确保您的系统资源不足。

@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
            }
        }
    }
}

如果您是Java 7+,可能需要查看try with resources