开发者

Java TCP/IP Server Closing Connections Improperly

I've created an MMO for the Android phone and use a Java server with TCP/IP sockets. Everything generally works fine, but after about a day of clients logging on and off my network becomes extremely laggy -- even if there aren't clients connected. NETSTAT shows no lingering connections, but there is obviously something terribly wrong going on.

If I do a full reboot everything magically is fine again, but this isn't a tenable solution for the long-term. This is what my disconnect method looks like (on both ends):

    public final void disconnect()
{
    Alive = false;
    Log.write("Disconnecting " + _socket.getRemoteSocketAddress());
    try
    {
        _socket.shutdownInput();
    }
    catch (final Exception e)
    {
        Log.write(e);
    }
    try
    {
        _socket.shutdownOutput();
    }
    catch (final Exception e)
    {
        Log.write(e);
    }
    try
    {
        _input.close();
    }
    catch (final Exception e)
    {
        Log.write(e);
    }
    try
    {
        _output.close();
    }
    catch (final Exception e)
    {
        Log.write(e);
    }
    try
    {
        _socket.close();
    }
    catch (final Exception e)
    {
        Log.write(e);
    }
}

_input and _output are BufferedInputStream and BufferedOutputStream spawned from the socket. According to documentation calling shutdownInput() and shutdownOutput() shouldn't be necessary, but I'm throwing everything I possibly can at this.

I instantiate the sockets with default settings -- I'm not touching soLinger, KeepAlive, noDelay or anything like that. I do not have any timeouts set on send/receive. I've tried using WireShark but it reveals nothing unusual, just like NETSTAT.

I'm pretty desperate for answers on this. I've put a 开发者_运维问答lot of effort into this project and am frustrated with what appears to be a serious hidden flaw in Java's default TCP implementation.


Get rid of shutdownInput() and shutdownOutput() and all the closes except the close for the BufferedOutputStream, and a subsequent close on the socket itself in a finally block as a belt & braces. You are shutting down and closing everything else before the output stream, which prevents it from flushing. Closing the output stream flushes it and closes the socket. That's all you need.


OP here, unable to comment on original post.

  • Restarting the server process does not appear to resolve the issue. The network remains very "laggy" even several minutes after shutting down the server entirely.

  • By "laggy" I mean the connection becomes extremely slow with both up and down traffic. Trying to load websites, or upload to my FTP, is painfully slow like I'm on a 14.4k modem (I'm on a 15mbs fiber). Internet Speed Tests don't even work when it is in this state -- I get an error about not finding the file, when the websites eventually load up.

  • All of this instantly clears up after a reboot, and only after a reboot.

  • I modified my disconnect method as EJP suggested, but the problem persists.

  • Server runs on a Windows 7 installation, latest version of Java / Java SDK. The server has 16gb of RAM, although it's possible I'm not allocating it properly for the JVM to use fully. No stray threads or processes appear to be present. I'll see what JVISUALVM says. – jysend 13 mins ago

  • Nothing unusual in JVISUALVM -- 10mb heap, 50% CPU use, 3160 objects (expected), 27 live threads out of 437 started. Server has been running for about 18 hours; loading up CNN's front page takes about a minute, and the normal speed test I use (first hit googling Speed Test) won't even load the page. NETSTAT shows no lingering connections. Ran all up to date antivirus. Server has run 24/7 in the past without any issues -- it is only when I started running this Java server on it that this started to happen.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜