java socket如何屏蔽ip_java socket 网络拦截 | 学步园

本程序用eclipse编辑在J2SDK6.0+WinPcap 3.1+Jpcap 0.5下编译运行

/*******************

* 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 packet) {

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

/*IP数据报报文头*/

byte[] l=packet.header;

/*

for (int t=0;t<21;t++){

System.out.print(l[t]+" *** ");

}

*/

String str="";

System.out.print("报文头 : ");

for (int i=0;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("首部长度 :"+(d*8)+"bit");

/*分析源IP地址和目的IP地址*/

/*分析协议类型*/

/**

if(packet.getClass().equals(IPPacket.class)) {

IPPacket 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("协议类型 :ARP协议");

try {

ARPPacket arpPacket = (ARPPacket)packet;

System.out.println("源网卡MAC地址为 :"+arpPacket.getSenderHardwareAddress());

System.out.println("源IP地址为 :"+arpPacket.getSenderProtocolAddress());

System.out.println("目的网卡MAC地址为 :"+arpPacket.getTargetHardwareAddress());

System.out.println("目的IP地址为 :"+arpPacket.getTargetProtocolAddress());

} catch( Exception e ) {

e.printStackTrace();

}

}

else

if(packet.getClass().equals(UDPPacket.class))

{

System.out.println("协议类型 :UDP协议");

try {

UDPPacket udpPacket = (UDPPacket)packet;

System.out.println("源IP地址为 :"+udpPacket.src_ip);

int tport = udpPacket.src_port;

System.out.println("源端口为:"+tport);

System.out.println("目的IP地址为 :"+udpPacket.dst_ip);

int lport = udpPacket.dst_port;

System.out.println("目的端口为:"+lport);

} catch( Exception e ) {

e.printStackTrace();

}

}

else

if(packet.getClass().equals(TCPPacket.class)) {

System.out.println("协议类型 :TCP协议");

try {

TCPPacket tcpPacket = (TCPPacket)packet;

int tport = tcpPacket.src_port;

System.out.println("源IP地址为 :"+tcpPacket.src_ip);

System.out.println("源端口为:"+tport);

System.out.println("目的IP地址为 :"+tcpPacket.dst_ip);

int lport = tcpPacket.dst_port;

System.out.println("目的端口为:"+lport);

} catch( Exception e ) {

e.printStackTrace();

}

}

else

if(packet.getClass().equals(ICMPPacket.class))

System.out.println("协议类型 :ICMP协议");

else

System.out.println("协议类型 :GGP、EGP、JGP协议或OSPF协议或ISO的第4类运输协议TP4");

/*IP数据报文数据*/

byte[] k=packet.data;

String str1="";

System.out.print("数据 : ");

for(int i=0;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("数据报类型 : "+packet.getClass());

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

}

public static void main(String[] args) throws Exception{

// TODO 自动生成方法存根

NetworkInterface[] devices = JpcapCaptor.getDeviceList();        //.getDeviceList();.

//for (int i =0; i

int a=0;

//try {

/*本地网络信息*/

byte[] b=devices[1].mac_address; //网卡物理地址

//}

//catch() {}

System.out.print("网卡MAC : 00");

for (int j=0;j

//a=a<<8;

a=b[j];

a=a<<24;

a=a>>>24;

System.out.print(Integer.toHexString(a));

}

System.out.println();

NetworkInterfaceAddress[] k=devices[1].addresses;

//System.out.println("网卡MAC : "+Integer.toHexString(a));

for(int n=0;n

System.out.println("本机IP地址 : "+k[n].address);     //本机IP地址

System.out.println("子网掩码   : "+k[n].subnet);      //子网掩码

}

System.out.println("网络连接类型 : "+devices[1].datalink_description);

//}

NetworkInterface deviceName = devices[1];

/*将网卡设为混杂模式下用网络设备deviceName*/

JpcapCaptor jpcap =JpcapCaptor.openDevice(deviceName, 2000, false, 1);           //openDevice(deviceName,1028,false,1);

jpcap.loopPacket(-1,new JpcapTip());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值