ChinaTelecom
V2EX  ›  Java

好好的 ServerSocket 久了会自己 Close 是什么情况.......

  •  
  •   ChinaTelecom · Aug 26, 2016 · 4627 views
    This topic created in 3722 days ago, the information mentioned may be changed or developed.

    核心逻辑其实巨简单,就是

    while true:
    	try
    		socket = serverSocket.accept
    		xxxx(socket)
     		......
    	catch
    		......
    

    这样的。。然后程序跑个几小时基本没问题,然而有时更久后在有新连接进来以后 accept 居然会报 SocketClosedException 出现也没什么规律 不过有个三四次了 确认程序没有任何其他地方 close 这个 而且真关了的 ServerSocket 不是应该调用 accept 时立即爆 Exception 么 这个居然是继续阻塞着每来个新连接报一次?!

    6 replies  •  2016-08-27 00:13:21 +08:00
    wizarot
    wizarot
    Aug 26, 2016
    1
    你试试增加个心跳信号来维持看看?
    可能是超时了.
    ChinaTelecom
    ChinaTelecom
    OP
    Aug 26, 2016
    2
    @wizarot accept 的超时我还特意设成 0 了...就是无限
    Comdex
    Comdex
    Aug 26, 2016
    3
    是不是 clientSocket 关闭了....
    chareice
    chareice
    Aug 26, 2016
    4
    心跳包,应用层的心跳包。
    owt5008137
    owt5008137
    Aug 26, 2016 via Android
    5
    倒是贴一下 Exception 的内容啊。看看 Message 什么的,能有调用栈更好
    leaves615
    leaves615
    Aug 27, 2016
    6
    链路黑洞
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1413 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 18:49 · PVG 02:49 · LAX 10:49 · JFK 13:49
    ♥ Do have faith in what you're doing.