How could I sniff network traffic in Java?

I just looked to find out how to make a program that would sniff my network traffic in Java, but I couldn't find anything. I wanted to know if there is a way to view network traffic. I heard about the idea with Socket , but I donโ€™t understand how this will work. Anyway, we're just looking for an API or a way to write it ourselves.

EDIT: I would love to use the API, but I would also like to clarify the way to sniff traffic using Socket.

+7
java sockets sniffing
source share
4 answers

jpcap , jNetPcap are pcap shell projects in Java.

Kraken is a similar project, well documented with many examples.

simple example from the Kraken website:

public static void main(String[] args) throws Exception { File f = new File("sample.pcap"); EthernetDecoder eth = new EthernetDecoder(); IpDecoder ip = new IpDecoder(); TcpDecoder tcp = new TcpDecoder(new TcpPortProtocolMapper()); UdpDecoder udp = new UdpDecoder(new UdpPortProtocolMapper()); eth.register(EthernetType.IPV4, ip); ip.register(InternetProtocol.TCP, tcp); ip.register(InternetProtocol.UDP, udp); PcapInputStream is = new PcapFileInputStream(f); while (true) { // getPacket() will throws EOFException and you should call is.close() PcapPacket packet = is.getPacket(); eth.decode(packet); } } 
+6
source share

Another libpcap shell for Java - https://github.com/kaitoy/pcap4j

Pcap4J is a Java library for collecting, processing, and sending packages. Pcap4J wraps its own packet capture library (libpcap or WinPcap) through JNA and provides you with Java-oriented APIs.

+4
source share

You need a batch api sniffer, maybe netutils is what you need:

The 'netutils' package provides a low-level java network library. Does it contain extensive infrastructure for sniffi? ng, injection, construction and analysis of Ethernet / IP / TCP / UDP / ICMP packets.

+1
source share

Not reporting either the API or Java, but if you really only want to sniff the data for analysis purposes, try: WireShark . His application is used for network analysis.

Useful if someone does not know.

0
source share

All Articles