I was just looking around 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 was any way
You need a packet sniffer api, maybe netutils is what you need:
The 'netutils' package gives a low level java network library. It contains extensive infrastructure for sniffing, injecting, building and parsing Ethernet/IP/TCP/UDP/ICMP packets.
Another Java libpcap wrapper is https://github.com/kaitoy/pcap4j
Pcap4J is a Java library for capturing, crafting and sending packets. Pcap4J wraps a native packet capture library (libpcap or WinPcap) via JNA and provides you Java-Oriented APIs.
jpcap, jNetPcap -- those are pcap wrapper projects in Java.
Kraken -- similar project, well documented with lots of examples.
simple example from the Kraken web site:
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);
}
}
Not telling any API or java related thing but if you really want to only sniff data for analysis purpose then give try: WireShark
. Its an application used for network analyse.
Its useful if someone is not aware of.