「ip工具类java」网络设备ip搜索工具
今天给各位分享ip工具类java的知识,其中也会对网络设备ip搜索工具进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java如何编程实现,获取固定IP发来所有的数据包 ?
- 2、如何在java中获取本地ip
- 3、表示IP地址的操作类是什么 java
- 4、Java 编写IP动态代理
- 5、JAVA如何将ip字符串转化为整型的工具方法
java如何编程实现,获取固定IP发来所有的数据包 ?
java获取固定IP发来所有的数据包,需要实现网络嗅探的部分功能:
代码如下;
/*******************
* 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;t21;t++){
System.out.print(l[t]+" *** ");
}
*/
String str="";
System.out.print("报文头 : ");
for (int i=0;il.length;i++) {
//str=str+l;
int m=0;
m=l[i];
m=m24;
m=m24;
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;ik.length;i++) {
//int m=0;
//m=k[i];
//m=m24;
//m=m24;
//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; idevices.length;i++) {
int a=0;
//try {
/*本地网络信息*/
byte[] b=devices[1].mac_address; //网卡物理地址
//}
//catch() {}
System.out.print("网卡MAC : 00");
for (int j=0;jb.length;j++){
//a=a8;
a=b[j];
a=a24;
a=a24;
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;nk.length;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());
}
}
如何在java中获取本地ip
很多朋友都想知道java如何获取本地ip?下面就一起来了解一下吧~
获取java本地ip一共有两种方法:1、inetAddress类;2、封装方法。
1、 inetAddress类
通过InetAddress的实例对象包含以数字形式保存的IP地址,同时还可能包含主机名(如果使用主机名来获取InetAddress的实例,或者使用数字来构造,并且启用了反向主机名解析的功能)。InetAddress类提供了将主机名解析为IP地址(或反之)的方法。其生成InetAddress对象的方法。
import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; public class Main { public static void main(String[] args) throws UnknownHostException { //Inet4Address address= (Inet4Address) Inet4Address.getLocalHost(); InetAddress address = InetAddress.getLocalHost(); System.out.println(address);//获取计算机名称和ip地址 String hostAddress = address.getHostAddress(); System.out.println(hostAddress);//获取ip地址 String hostName = address.getHostName(); System.out.println(hostName);//获取计算机名称 } }
2、封装方法。
public static String getLocalIp() { Enumeration netInterfaces = null; try { netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface nif = netInterfaces.nextElement(); Enumeration InetAddress = nif.getInetAddresses(); while (InetAddress.hasMoreElements()) { String ip = InetAddress.nextElement().getHostAddress(); if (ip.startsWith("192.168")) { return ip; } } } } catch (SocketException e) { } return "127.0.0.1"; }
表示IP地址的操作类是什么 java
InetAddress
它有好几个静态方法可以获得IP地址,主机名之类的。
具体的可以参考API。
下面是我以前写的一个测试IP的类。
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
*本程序主要测试IP地址……
*/
public class Test {
public static void main(String[]args){
InetAddress localHost = null;
try{
localHost = InetAddress.getLocalHost();
}
catch(UnknownHostException e){
}
String localHostName = localHost.getHostName();
System.out.println("本地主机名为: " + localHostName);
byte[] byteAddress = localHost.getAddress(); //本地主机IP地址的字节表示
int[] intArr = new int[byteAddress.length];
for(int i=0;ibyteAddress.length;i++){
//intArr[i] = (byteAddress[i]24)24;
intArr[i] = byteAddress[i] 0xFF;
}
for(int i=0;ibyteAddress.length;i++){
if(i==byteAddress.length-1){
System.out.println(intArr[i]);
break;
}
System.out.print(intArr[i] + ".");
}
InetAddress address1 = null;
try {
address1 = InetAddress.getByName("172.25.67.65");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(address1.getHostName());
}
}
Java 编写IP动态代理
package com.tan.test;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.log4j.Logger;
public class TestProxyIp {
private static final Logger log = Logger.getLogger(TestProxyIp.class);
public static void main(String[] args) throws IOException {
System.setProperty("http.maxRedirects", "50");
System.getProperties().setProperty("proxySet", "true");
// 如果不设置,只要代理IP和代理端口正确,此项不设置也可以
String ip = "59.175.192.126";
ip = "221.214.180.130";
ip = "122.224.171.91";
ip = "58.221.213.166";
ip = "202.106.16.36";
ip = "121.8.191.34";
ip = "222.208.242.30";
ip = "219.239.90.85";
ip = "60.31.177.188";
System.getProperties().setProperty("http.proxyHost", ip);
System.getProperties().setProperty("http.proxyPort", "3128");
//确定代理是否设置成功
log.info(getHtml(""));
//log.info(getHtml(""));
}
private static String getHtml(String address){
StringBuffer html = new StringBuffer();
String result = null;
try{
URL url = new URL(address);
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");
BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
try{
String inputLine;
byte[] buf = new byte[4096];
int bytesRead = 0;
while (bytesRead = 0) {
inputLine = new String(buf, 0, bytesRead, "UTF-8");
/*if (!"".equals(inputLine)) {
System.out.println(inputLine);
}*/
html.append(inputLine);
bytesRead = in.read(buf);
inputLine = null;
}
buf = null;
}finally{
in.close();
conn = null;
url = null;
}
//result = new String(html.toString().trim().getBytes("ISO-8859-1"), "UTF-8").toLowerCase();
//result=new String(html.toString().trim().getBytes("ISO-8859-1"), "GBK");
}catch (Exception e) {
e.printStackTrace();
return null;
}/*finally{
html = null;
}*/
return html.toString();
}
}
但是找不到有用的动态ip。
JAVA如何将ip字符串转化为整型的工具方法
这个是测试string 和int 转化的问题,其中string是不能直接转化为int类型的,所以需要转化为int的对象类型Integer ,然后Integer 自动拆箱,转化为int。
num=Integer.parseInt(str);
num=Integer.valueOf(str); 这俩的作用是一样的,是把String 的"12" ,转化为整形 的12。
这段代码是没问题的,但是你要转成整形,字符串“qwee”这是转不了的,所以只要你输入数字就没问题。
关于ip工具类java和网络设备ip搜索工具的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。