java如何编程实现,获取固定IP发来所有的数据包

java如何编程实现,获取固定IP发来所有的数据包,第1张

java获取固定IP发来所有的数据包,需要实现网络嗅探的部分功能:

代码如下;

/

 JpcapTipjava

/

package m;

import jpcapPacketReceiver;

import jpcapJpcapCaptor;

import jpcappacket;

import jpcapNetworkInterface;

import jpcapNetworkInterfaceAddress;

//import javanetInetAddress;

//import javanetUnknownHostException;

public class JpcapTip implements PacketReceiver {

public void receivePacket(Packet packet) {

   Systemoutprintln("");

   /IP数据报报文头/

   byte[] l=packetheader;     

   /

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

    Systemoutprint(l[t]+"  ");

   }

   /

   String str="";

    Systemoutprint("报文头 : ");

    for (int i=0;i<llength;i++) {

     //str=str+l;

     int m=0;

     m=l[i];

     m=m<<24;

     m=m>>>24;

     str=str+IntegertoHexString(m);

     //Systemoutprint("          "+l[i]);

    }

   Systemoutprintln(str);

   int d=llength;

   Systemoutprintln("首部长度 :"+(d8)+"bit");

  

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

   /分析协议类型/

   /

   if(packetgetClass()equals(IPPacketclass)) {

    IPPacket ipPacket=(IPPacket)packet;

    byte[] iph=ipPacketoption;

    String iphstr=new String(iph);

    Systemoutprintln(iphstr);

   }

   /

   if(packetgetClass()equals(ARPPacketclass))

   {

    Systemoutprintln("协议类型 :ARP协议");

    try {

     ARPPacket arpPacket = (ARPPacket)packet;

     Systemoutprintln("源网卡MAC地址为 :"+arpPacketgetSenderHardwareAddress());

     Systemoutprintln("源IP地址为 :"+arpPacketgetSenderProtocolAddress());

     Systemoutprintln("目的网卡MAC地址为 :"+arpPacketgetTargetHardwareAddress());

     Systemoutprintln("目的IP地址为 :"+arpPacketgetTargetProtocolAddress());

} catch( Exception e ) {

     eprintStackTrace();

    } 

   }

   else 

    if(packetgetClass()equals(UDPPacketclass))

    {

     Systemoutprintln("协议类型 :UDP协议");

     try {

      UDPPacket udpPacket = (UDPPacket)packet;

      Systemoutprintln("源IP地址为 :"+udpPacketsrc_ip);

      int tport = udpPacketsrc_port;

      Systemoutprintln("源端口为:"+tport);

      Systemoutprintln("目的IP地址为 :"+udpPacketdst_ip);

      int lport = udpPacketdst_port;

      Systemoutprintln("目的端口为:"+lport);

     } catch( Exception e ) {

      eprintStackTrace();

     } 

    }

   else

    if(packetgetClass()equals(TCPPacketclass)) {

     Systemoutprintln("协议类型 :TCP协议");

     try {

      TCPPacket tcpPacket = (TCPPacket)packet;

      int tport = tcpPacketsrc_port;

      Systemoutprintln("源IP地址为 :"+tcpPacketsrc_ip);

      Systemoutprintln("源端口为:"+tport);

      Systemoutprintln("目的IP地址为 :"+tcpPacketdst_ip);

      int lport = tcpPacketdst_port;

      Systemoutprintln("目的端口为:"+lport);

     } catch( Exception e ) {

      eprintStackTrace();

     }

    }

   else

    if(packetgetClass()equals(ICMPPacketclass))

     Systemoutprintln("协议类型 :ICMP协议");

   else

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

/IP数据报文数据/

   byte[] k=packetdata;   

   String str1="";

    Systemoutprint("数据 : ");

     for(int i=0;i<klength;i++) {

      //int m=0;

      //m=k[i];

      //m=m<<24;

      //m=m>>>24;

      //str1=str+IntegertoHexString(m);

      str1 = new String(k);

      //str1=str1+k[i];

      //Systemoutprint("          "+k[i]);

     }

     Systemoutprintln(str1);

   Systemoutprintln("数据报类型 : "+packetgetClass());

   Systemoutprintln("");

}

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

   // TODO 自动生成方法存根

  

   NetworkInterface[] devices = JpcapCaptorgetDeviceList();        //getDeviceList();

   //for (int i =0; i<deviceslength;i++) {

    int a=0;

    //try {

    /本地网络信息/

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

    //}

    //catch() {}

    Systemoutprint("网卡MAC : 00");   

    for (int j=0;j<blength;j++){

     //a=a<<8;

     a=b[j];

     a=a<<24;

     a=a>>>24;

     Systemoutprint(IntegertoHexString(a));

    }

    Systemoutprintln();

    NetworkInterfaceAddress[] k=devices[1]addresses;

   

    //Systemoutprintln("网卡MAC : "+IntegertoHexString(a));

    for(int n=0;n<klength;n++) {

     Systemoutprintln("本机IP地址 : "+k[n]address);     //本机IP地址

     Systemoutprintln("子网掩码   : "+k[n]subnet);      //子网掩码

    }

    Systemoutprintln("网络连接类型 : "+devices[1]datalink_description);

   //}

   NetworkInterface deviceName = devices[1];

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

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

   jpcaploopPacket(-1,new JpcapTip());

}

}

   Java中可以使用程序来获取本地ip地址和mac地址,使用InetAddress这个工具类,示例如下:

import javanet;

public class NetInfo {

 public static void main(String[] args) {

    new NetInfo()say();

    }

 public void say() {

   try {

   InetAddress i = InetAddressgetLocalHost();

   Systemoutprintln(i);                  //计算机名称和IP

   Systemoutprintln(igetHostName());    //名称

   Systemoutprintln(igetHostAddress()); //只获得IP

   }

   catch(Exception e){eprintStackTrace();}

 }

}

    也可以通过命令行窗口来查看本地ip和mac地址,输入命令:ipconfig。

依次双击打开两个连接图标,同时查看连接状态中的数据变化,再打开一个网页浏览,一般发送和接收两个数据都反映强烈的,表明该网络信息流量就大,反映大的应该是正在运行中的网络。试试看哦。

qq494202907

import javanetInetAddress;

import javanetNetworkInterface;

import javanetSocketException;

import javanetUnknownHostException;

/

  物理地址是48位,别和ipv6搞错了

 /

public class LOCALMAC {

/

  @param args

  @throws UnknownHostException 

  @throws SocketException 

 /

public static void main(String[] args) throws UnknownHostException, SocketException {

// TODO Auto-generated method stub

//得到IP,输出PC-201309011313/1222067383

InetAddress ia = InetAddressgetLocalHost();

Systemoutprintln(ia);

getLocalMac(ia);

}

private static void getLocalMac(InetAddress ia) throws SocketException {

// TODO Auto-generated method stub

//获取网卡,获取地址

byte[] mac = NetworkInterfacegetByInetAddress(ia)getHardwareAddress();

Systemoutprintln("mac数组长度:"+maclength);

StringBuffer sb = new StringBuffer("");

for(int i=0; i<maclength; i++) {

if(i!=0) {

sbappend("-");

}

//字节转换为整数

int temp = mac[i]&0xff;

String str = IntegertoHexString(temp);

Systemoutprintln("每8位:"+str);

if(strlength()==1) {

sbappend("0"+str);

}else {

sbappend(str);

}

}

Systemoutprintln("本机MAC地址:"+sbtoString()toUpperCase());

}

}

SystemgetProperties()

返回的是Properties

Properties 类表示了一个持久的属性集

以下的代码就可以把Properties的内容读出来

Properties props = new Properties();

try {

InputStream in = new BufferedInputStream (new FileInputStream(filePath));

propsload(in);

Enumeration en = propspropertyNames();

while (enhasMoreElements()) {

String key = (String) ennextElement();

String Property = propsgetProperty (key);

Systemoutprintln(key+Property);

}

} catch (Exception e) {

eprintStackTrace();

}

对于补充的回答:

java不能直接得到楼主需要的信息,但是可以采用JNI,即调用C程序,让C得到这些信息

以上就是关于java如何编程实现,获取固定IP发来所有的数据包 全部的内容,包括:java如何编程实现,获取固定IP发来所有的数据包 、java如何查询本机ip地址和mac地址、电脑含有多个网卡,如何在java程序中判断哪个网卡正在使用,当由有线连接上网改变为无线时又该怎么判断。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9517368.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存