Java uses the Jpcap grab tool to obtain all TCP requests sent by this machine, and can debug the relevant HTTP request or SQL connection request sent by an application after startup according to the content of the TCP request, so that we can debug our program very well. It's good. An application without intrusive debugging the native sent HTTP interface, SQL connection interface and so on.
Import java.io.UnsupportedEncodingException;
Import java.net.InetAddress;
Import jpcap.*;
Import Jpcap.packet.EthernetPacket;
Import Jpcap.packet.Packet;
Import Jpcap.packet.TCPPacket;
Class Tcpdump implements Packetreceiver {public void Receivepacket (Packet Packet) {//System.out.println (Packet);
if (packet instanceof tcppacket) {tcppacket tcppacket = (tcppacket) packet; if (TcpPacket.src_ip.getHostAddress (). Equals (GetIP ()) {Ethernetpacket Ethernetpacket = (ethernetpacket)
Packet.datalink; System.out.print ("Source IP:" + tcppacket.src_ip + "Destination IP:" + tcppacket.dst_ip + "send port:" + Tcppacket.src_port + "Then
Receiving port: "+ tcppacket.dst_port +" \ n "); System.out. Print ("Source mac:" + ethernetpacket.getsourceaddress () + "Purpose Mac:" + Ethernetpacket.getdestin
Ationaddress () + "\ n");
System.out.print ("agreement:" + tcppacket.protocol + "\ n");
System.out.print ("Data: \ n");
try {System.out.println (new String (Tcppacket.data, "utf-8")); } catch (Unsupportedencodingexception e) {//TODO auto-generated catch block E.printstacktrace ();
}//for (int i = 0; i < tcpPacket.data.length; i++)//System.out.print ((char) tcppacket.data[i));
}} public static String GetIP () {inetaddress inet = null;
try {inet = Inetaddress.getlocalhost (); String IP = inet.gethostaddress ();
The IP that is taken on some Linux machines is the host-bound native IP, that is, the 127.0.0.1 return IP;
}catch (Exception e) {} return null; }}<span style= "White-space:normal; Background-color: #ffffff; " > </span>
I start with an application to a query database, and then start the following program,
The output results are as follows: