java - 服务器在一段时间后停止接受连接 [英] java - server stops accepting connections after a while

查看:451
本文介绍了java - 服务器在一段时间后停止接受连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WinServer 2008计算机上有一个简单的多客户端Java服务器并且它工作正常,但在看似随机的时间(天)后,服务器停止接受任何连接,我无法弄清楚原因。

I have a simple multi-client java server on a WinServer 2008 machine and it's working fine, but after a seemingly random ammount of time (days) the server stops accepting any connections and i can't figure out why.

它没有捕获任何异常,它没有崩溃,它只是不接受任何连接,我认为这是serversocket是GC'd所以我做了它全球但无济于事。它似乎也不是TCP耗尽,因为没有任何人连接几个小时,它仍然不接受任何连接,我也将setReuseAddress设置为true但它仍然做同样的事情。

It doesn't catch any exceptions, it doesn't crash, it just doesn't accept any connections, i thought it was the serversocket being GC'd so i made it global but to no avail. It does not seem to be TCP exhaustion either, since there hasn't been anyone connected for hours and it still doesn't accept any connections and i also have set the setReuseAddress to true but it keeps doing the same thing.

让它再次运行的唯一方法是重新启动java服务器应用程序(不需要重启机器)并且它再次正常工作......一段时间。

The only way to make it work again is to reboot the java server application (doesn't need to reboot the machine) and it's working fine again...for a while.

这里是与我的情况相关的代码:

here's my code relevant to the situation:

        try
    {
        ServerSocket srvr = new ServerSocket(PORT);

        while (isAlive)
        {
            Socket skt = srvr.accept();

            this.bw = new PrintWriter(skt.getOutputStream(), true);
            this.br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            String msg = br.readLine();

                            //process msg

            skt.close();
        }
    }
    catch (Exception e)
    {
        System.out.println("Error");
    }

编辑:更改代码

            while (isAlive)
        {
            Socket skt = srvr.accept();
            skt.setSoTimeout(10000);

            acceptConnection(skt);
        }

public void acceptConnection(final Socket skt) throws Exception
{
    Thread discConn = new Thread()
    {
        public void run()
        {
            try
            {
                PrintWriter bw = new PrintWriter(skt.getOutputStream(), true);
                BufferedReader br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
                String msg = br.readLine();
//process msg
                }
            catch (SocketTimeoutException ste)
            {
                //
            }
            catch (SocketException se)
            {
                //
            }
            catch (Exception e)
            {
                System.out.println("ERROR");
            }
            finally
            {
                try
                {
                    skt.close();
                }
                catch (IOException e)
                {
                    System.out.println("ERROR");
                }
            }
            }
    };
    discConn.start();


推荐答案

您在接受客户端I / O时阻止环。一个从不发送任何东西的客户端可以停止整个系统。你应该在接受循环中做的只是接受连接和启动线程。唯一的阻塞操作应该是accept()本身。客户端I / O应该在一个单独的线程中完成。

You are blocking on client I/O in the accept loop. One client that never sends anything can stop the whole system. All you should do in the accept loop is accept connections and start threads. The only blocking operation should be the accept() itself. Client I/O should be done in a separate thread.

这篇关于java - 服务器在一段时间后停止接受连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆