Java Socket network Interception

Source: Internet
Author: User

This program is compiled and run in j2sdk6.0 + Winpcap 3.1 + jpcap 0.5 by using eclipse.

/*******************
* Jpcaptip. Java
*/
Package m;

Import jpcap. packetreceiver;
Import jpcap. jpcapcaptor;
Import jpcap. packet .*;
Import jpcap. networkinterface;
Import jpcap. networkinterfaceaddress;
// Import java.net. inetaddress;
// Import java.net. unknownhostexception;

Public class jpcaptip implements packetreceiver {

Public void receivepacket (packet ){
System. out. println ("************************************* *******");
/* IP datagram packet header */
Byte [] L = packet. header;
/*
For (int t = 0; t <21; t ++ ){
System. Out. Print (L [T] + "***");
}
*/
String STR = "";
System. Out. Print ("packet header :");
For (INT I = 0; I <L. length; I ++ ){
// STR = STR + L;
Int m = 0;
M = L [I];
M = m <24;
M = m> 24;
STR = STR + integer. tohexstring (m );
// System. Out. Print ("***" + L [I]);
}
System. Out. println (STR );
Int d = L. length;
System. Out. println ("Header Length:" + (D * 8) + "bit ");

/* Analyze the source IP address and destination IP address */
/* Analyze the protocol type */
/**
If (packet. getclass (). Equals (ippacket. Class )){
Ippacket = (ippacket) packet;
Byte [] IPH = ippacket. option;
String iphstr = new string (IPH );
System. Out. println (iphstr );
}
*/
If (packet. getclass (). Equals (arppacket. Class ))
{
System. Out. println ("protocol type: ARP Protocol ");
Try {
Arppacket = (arppacket) packet;
System. Out. println ("Source Nic MAC address:" + arppacket. getsenderhardwareaddress ());
System. Out. println ("Source IP Address:" + arppacket. getsenderprotocoladdress ());
System. Out. println ("target Nic MAC address:" + arppacket. gettargethardwareaddress ());
System. Out. println ("Destination IP Address:" + arppacket. gettargetprotocoladdress ());


} Catch (exception e ){
E. printstacktrace ();
}
}
Else
If (packet. getclass (). Equals (udppacket. Class ))
{
System. Out. println ("protocol type: UDP ");
Try {
Udppacket = (udppacket) packet;
System. Out. println ("Source IP Address:" + udppacket. src_ip );
Int tport = udppacket. src_port;
System. Out. println ("Source Port:" + tport );
System. Out. println ("Destination IP Address:" + udppacket. dst_ip );
Int lport = udppacket. dst_port;
System. Out. println ("Destination Port:" + lport );
} Catch (exception e ){
E. printstacktrace ();
}
}
Else
If (packet. getclass (). Equals (tcppacket. Class )){
System. Out. println ("protocol type: TCP ");
Try {
Tcppacket = (tcppacket) packet;
Int tport = tcppacket. src_port;
System. Out. println ("Source IP Address:" + tcppacket. src_ip );
System. Out. println ("Source Port:" + tport );
System. Out. println ("Destination IP Address:" + tcppacket. dst_ip );
Int lport = tcppacket. dst_port;
System. Out. println ("Destination Port:" + lport );
} Catch (exception e ){
E. printstacktrace ();
}
}
Else
If (packet. getclass (). Equals (icmppacket. Class ))
System. Out. println ("protocol type: ICMP ");
Else
System. Out. println ("protocol type: GGP, EGP, jgp, OSPF, or ISO Transport Protocol tp4 ");



/* IP data packet data */
Byte [] K = packet. Data;
String str1 = "";
System. Out. Print ("data :");
For (INT I = 0; I <K. length; I ++ ){
// Int m = 0;
// M = K [I];
// M = m <24;
// M = m >>> 24;
// Str1 = STR + integer. tohexstring (m );
Str1 = new string (k );
// Str1 = str1 + k [I];
// System. Out. Print ("***" + k [I]);
}
System. Out. println (str1 );
System. Out. println ("datagram type:" + packet. getclass ());

System. out. println ("************************************* *******");
}

Public static void main (string [] ARGs) throws exception {
// Todo automatically generates method stubs

Networkinterface [] devices = jpcapcaptor. Getdevicelist (); //. Getdevicelist ();.
// For (INT I = 0; I <devices. length; I ++ ){
Int A = 0;
// Try {
/* Local network information */
Byte [] B = devices [1]. mac_address; // the physical address of the NIC.
//}
// Catch (){}
System. Out. Print ("Nic Mac: 00 ");
For (Int J = 0; j <B. length; j ++ ){
// A = A <8;
A = B [J];
A = A <24;
A = A >>> 24;
System. Out. Print (integer. tohexstring ());
}
System. Out. println ();
Networkinterfaceaddress [] K = devices [1]. addresses;

// System. Out. println ("Nic Mac:" + integer. tohexstring ());
For (INT n = 0; n <K. length; n ++ ){
System. Out. println ("local IP Address:" + k [N]. Address); // local IP Address
System. Out. println ("subnet mask:" + k [N]. subnet); // Subnet Mask
}
System. Out. println ("network connection type:" + devices [1]. datalink_description );
//}
Networkinterface devicename = devices [1];
/* Set the NIC to devicename of the network device in mixed mode */
Jpcapcaptor jpcap = jpcapcaptor. opendevice (devicename, 2000, false, 1); // opendevice (devicename, 1028, false, 1 );
Jpcap. looppacket (-1, new jpcaptip ());
}

}

 

 

From: http://hi.baidu.com/wr5666/blog/item/a50e32cf03e34e3fb700c878.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.