简体   繁体   English

使用Java发送零数据TCP / IP数据包

[英]Send a zero-data TCP/IP packet using Java

My goal is to send a TCP packet with empty data field, in order to test the socket with the remote machine. 我的目标是发送一个带有空数据字段的TCP数据包,以便用远程机器测试套接字。

I am using the OutputStream class's method of write(byte[] b). 我正在使用OutputStream类的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. 如果“”被“”或任何非零字符串替换,它可以正常工作。

I tried jpcap, which worked with me, but didn't serve my goal. 我尝试了jpcap,它与我合作,但没有达到我的目标。 I thought of extending the OutputStream class, and implementing my own OutputStream.write method. 我想扩展OutputStream类,并实现我自己的OutputStream.write方法。 But this is beyond my knowledge. 但这超出了我的意识。 Please give me advice if someone have already done such a thing. 如果有人已经做过这样的事情,请给我建议。

If you just want to quickly detect silently dropped connections, you can use Socket.setKeepAlive( true ) . 如果您只想快速检测静默删除的连接,可以使用Socket.setKeepAlive( true ) This method ask TCP/IP to handle heartbeat probing without any data packets or application programming. 此方法要求TCP / IP在没有任何数据包或应用程序编程的情况下处理心跳探测。 If you want more control on the frequency of the heartbeat, however, you should implement heartbeat with application level packets. 但是,如果您想要更多地控制心跳的频率,则应该使用应用程序级别的数据包实现心跳。

See here for more details: http://mindprod.com/jgloss/socket.html#DISCONNECT 有关更多详细信息,请参见此处: http//mindprod.com/jgloss/socket.html#DISCONNECT

There is no such thing as an empty TCP packet, because there is no such thing as a TCP packet. 没有空TCP数据包这样的东西,因为没有TCP数据包这样的东西。 TCP is a byte-stream protocol. TCP是字节流协议。 If you send zero bytes, nothing happens. 如果发送零字节,则没有任何反应。

To detect broken connections, short of keepalive which only helps you after two hours, you must rely on read timeouts and write exceptions. 要检测断开的连接,缺少keepalive,这只能在两小时后帮助您,您必须依赖读取超时并编写异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM