使用 Java 发送零数据 TCP/IP 数据包 [英] Send a zero-data TCP/IP packet using Java

查看:60
本文介绍了使用 Java 发送零数据 TCP/IP 数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是发送一个带有空数据字段的 TCP 数据包,以测试与远程机器的套接字.

My goal is to send a TCP packet with empty data field, in order to test the socket with the remote machine.

我正在使用 OutputStream 类的 write(byte[] b) 方法.我的尝试:

I am using the OutputStream class's method of write(byte[] b). my attempt:

outClient = ClientSocket.getOutputStream();
outClient.write(("").getBytes());

这样做,数据包永远不会出现在线路上.如果将 "" 替换为 " " 或任何非零字符串,它会正常工作.

Doing so, the packet never show up on the wire. It works fine if "" is replaced by " " or any non-zero string.

我尝试了 jpcap,它对我有用,但没有达到我的目标.我想扩展 OutputStream 类,并实现我自己的 OutputStream.write 方法.但这超出了我的知识范围.如果有人已经做过这样的事情,请给我建议.

I tried jpcap, which worked with me, but didn't serve my goal. I thought of extending the OutputStream class, and implementing my own OutputStream.write method. But this is beyond my knowledge. Please give me advice if someone have already done such a thing.

推荐答案

如果您只想快速检测静默断开的连接,可以使用 Socket.setKeepAlive( true ).这种方法要求 TCP/IP 在没有任何数据包或应用程序编程的情况下处理心跳探测.但是,如果您想对心跳频率进行更多控制,则应使用应用程序级数据包实现心跳.

If you just want to quickly detect silently dropped connections, you can use Socket.setKeepAlive( true ). This method ask TCP/IP to handle heartbeat probing without any data packets or application programming. If you want more control on the frequency of the heartbeat, however, you should implement heartbeat with application level packets.

请参阅此处了解更多详情:http://mindprod.com/jgloss/socket.html#断开连接

See here for more details: http://mindprod.com/jgloss/socket.html#DISCONNECT

这篇关于使用 Java 发送零数据 TCP/IP 数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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