`
hpq852
  • 浏览: 1656 次
社区版块
存档分类
最新评论

Socket Disconnet Detection (ZZ)

阅读更多

Disconnet Detection

Since TCP/IP sends no packets except when there is traffic, without Socket.setKeepAlive( true ), it has no way of noticing a disconnect until you start trying to send (or to a certain extent receive) traffic again. Java has the Socket.setKeepAlive( true ) method to ask TCP/IP to handle heartbeat probing without any data packets or application programming. Unfortunately, you can't tell it how frequently to send the heartbeat probes. If the other end does not respond in time, you will get a socket exception on your pending read. Heartbeat packets in both directions let the other end know you are still there. A heartbeat packet is just an ordinary TCP/IP ack packet without any piggybacking data.

When the applications are idling, your applications could periodically send tiny heartbeat messages to each other. The receiver could just ignore them. However, they force the TCP/IP protocol to check if the other end is still alive. These are not part of the TCP/IP protocol. You would have to build them into your application protocols. They act as are-you-still-alive? messages. I have found Java's connection continuity testing to be less that 100% reliable. My bullet-proof technique to detect disconnect is to have the server send an application-level heartbeat packet if it has not sent some packet in the last 30 seconds. It has to send some message every 30 seconds, not necessarily a dummy heartbeat packet. The heartbeat packets thus only appear when the server is idling. Otherwise normal traffic acts as the heartbeat. The Applet detects the lack of traffic on disconnect and automatically restarts the connection. The downside is your applications have to be aware of these heartbeats and they have to fit into whatever other protocol you are using, unlike relying on TCP/IP level heartbeats.

However, it is simpler to use the built-in Socket.setKeepAlive( true ) method to ask TCP/IP to handle the heartbeat probing without any data packets or application programming. Each end with nothing to say just periodically sends an empty data packet with its current sequence, acknowledgement and window numbers.

The advantage of application level heartbeats is they let you know the applications at both ends are alive, not just the communications software.

分享到:
评论

相关推荐

    iocp封装类

    iocp完成端口的封装,使用TCP的IOCP实现,可以传送大消息包、文件,同一客户端可以同时发送多个文件 // 1、用TClients创建一个对象,...// 5、调用pClients->Disconnet(...)主动断开连接 // 6、销毁pClients对象

    ping-tcp-js:ping-js

    欢迎来到 :waving_hand: 安装 npm install ping-tcp-js ping 参数 ... const port = 80; client.ping(host, port)... .catch((e) => console.error('not disconnet', e)); 网址 const client = require('ping-tcp-js');

    TCP-UDP连接断开程序.zip

    在IT领域,网络通信是计算机之间交流的基础,TCP(传输控制协议)和UDP(用户数据报协议)是两种最常用的数据传输协议。本程序“TCP-UDP连接断开程序”专注于帮助用户查看并管理本地的TCP和UDP连接,以便进行网络...

    2013年全省第三方网优测试服务考试题(大理更新).pdf

    **城市语音DT测试**:掉话次数的计算涉及到“Disconnet”或“Channel Release”消息,以及通话模式转为空闲模式的情况。\n\n9. **GSM频率范围**:GSM900的上行频率范围是890-915MHz,下行频率范围是935-960MHz。\n\...

    oracle第二天学习笔记 

    - 断开连接:`disconnet;` - 修改密码:`alter user username identified by new_password;` - 显示当前用户:`show user;` - 退出SQL*Plus:`exit;` ### 文件和脚本处理 - 执行SQL脚本文件: - 使用`.start`命令...

    oracle学习

    2. **断开连接命令** (`disconnet`): - 断开当前数据库连接。 3. **密码修改命令** (`password`): - 修改用户的密码。 - 需要使用 `sys` 或 `system` 身份登录。 4. **显示当前用户命令** (`show user`): -...

    2013年全省第三方网优测试服务考试题(大理更新).docx

    - **定义**:在城市话音DT测试中,如果通话中出现Disconnet或ChannelRelease中的任意一条消息,计为一次呼叫正常释放;只有当这两条消息都未出现而由通话模式转为空闲模式时,才计为一次掉话。这样的定义可以帮助更...

Global site tag (gtag.js) - Google Analytics