简体   繁体   English

如何使用Java代码区分机器中的TCP和UDP数据包?

[英]How to differentiate TCP and UDP packets in machine with java code?

How to differentiate the TCP and UDP packets before sending it to the internet using java? 在使用Java发送到Internet之前,如何区分TCP和UDP数据包? I need to listen to those packets and differentiate whether it is TCP or UDP. 我需要听那些数据包并区分是TCP还是UDP。 How to achieve this using Java code? 如何使用Java代码实现这一目标?

UPDATE: I need to redirect all the TCP/IP and UDP packects to a server (Ip) and need to track the traffic of the TCP/IP and UDP Packets and make a log. 更新:我需要将所有TCP / IP和UDP包重定向到服务器(Ip),并且需要跟踪TCP / IP和UDP数据包的流量并进行日志记录。

You can't see individual packet with TCP in Java but when you are using a Socket it will be TCP and if you are using a DatagramSocket it will be UDP. 在Java中,您看不到带有TCP的单个数据包,但是当您使用Socket时,它将是TCP,如果您使用DatagramSocket,则它将是UDP。 ie the classes are different so there is no hcance you will confuse them. 也就是说,类是不同的,所以没有机会将它们混淆。

If you want to snoop a network adapter to see which type of packets are being sent, you can use JPCap. 如果要监听网络适配器以查看正在发送的数据包类型,则可以使用JPCap。

If you are listening for the packets on a socket, you need to specify the socket type. 如果您正在侦听套接字上的数据包,则需要指定套接字类型。

java.net.DatagramSocket for UDP, and java.net.Socket for TCP, so you will always know what type of packet you are getting. java.net.DatagramSocket的UDP和java.net.Socket的TCP,那么你将永远知道你做了什么类型的数据包。 Or are you looking to sniff traffic, and differentiate that way. 还是您想嗅探流量并以这种方式区分。

if you can read the ip packet header info, you can get it done. 如果您可以读取ip数据包标头信息,则可以完成。 In ip packet header includes the next protocol no(6 means TCP,17 means UDP), you should use this to differentiate. 在ip数据包标头中包含下一个协议编号(6表示TCP,17表示UDP),您应该使用它来区分。

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

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