导航:首页 > 编程语言 > java解析ip数据包

java解析ip数据包

发布时间:2024-01-02 15:58:23

㈠ 请问java获取请求对象的电脑ip和网络ip的方法是什么

importjava.io.*;
importjava.net.*;

try{
URLurl=newURL("http://checkip.amazonaws.com");
BufferedReaderbr=newBufferedReader(newInputStreamReader(url.openStream()));
StringwanIP=br.readLine();
System.out.println("WANIP:"+wanIP);

InetAddresslanIP=Inet4Address.getLocalHost();//orInet6Address.getLocalHost();
System.out.println("LANIP:"+lanIP.getHostAddress());
}catch(Exceptione){
e.printStackTrace();
}

㈡ java怎么获取请求的ip

java获取外网ip地址方法:
public class Main {

public static void main(String[] args) throws SocketException {
System.out.println(Main.getRealIp());
}

public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP

Enumeration<NetworkInterface> netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 外网IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 内网IP
localip = ip.getHostAddress();
}
}
}

if (netip != null && !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
}

㈢ java中如何获取到本机的外网ip地址

java获取本机的外网ip示例:
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 获取本机外网IP地址
* 思想是访问网站http://checkip.dyndns.org/,得到返回的文本后解析出本机在外网的IP地址
* @author pieryon
*
*/
public class ExternalIpAddressFetcher {
// 外网IP提供者的网址
private String externalIpProviderUrl;

// 本机外网IP地址
private String myExternalIpAddress;

public ExternalIpAddressFetcher(String externalIpProviderUrl) {
this.externalIpProviderUrl = externalIpProviderUrl;

String returnedhtml = fetchExternalIpProviderHTML(externalIpProviderUrl);

parse(returnedhtml);
}

/**
* 从外网提供者处获得包含本机外网地址的字符串
* 从http://checkip.dyndns.org返回的字符串如下
* <html><head><title>Current IP Check</title></head><body>Current IP Address: 123.147.226.222</body></html>
* @param externalIpProviderUrl
* @return
*/
private String fetchExternalIpProviderHTML(String externalIpProviderUrl) {
// 输入流
InputStream in = null;

// 到外网提供者的Http连接
HttpURLConnection httpConn = null;

try {
// 打开连接
URL url = new URL(externalIpProviderUrl);
httpConn = (HttpURLConnection) url.openConnection();

// 连接设置
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");

// 获取连接的输入流
in = httpConn.getInputStream();
byte[] bytes=new byte[1024];// 此大小可根据实际情况调整

// 读取到数组中
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=in.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}

// 将字节转化为为UTF-8的字符串
String receivedString=new String(bytes,"UTF-8");

// 返回
return receivedString;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
httpConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}

// 出现异常则返回空
return null;
}

/**
* 使用正则表达式解析返回的HTML文本,得到本机外网地址
* @param html
*/
private void parse(String html){
Pattern pattern=Pattern.compile("(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})", Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(html);
while(matcher.find()){
myExternalIpAddress=matcher.group(0);
}
}

/**
* 得到本机外网地址,得不到则为空
* @return
*/
public String getMyExternalIpAddress() {
return myExternalIpAddress;
}

public static void main(String[] args){
ExternalIpAddressFetcher fetcher=new ExternalIpAddressFetcher("http://checkip.dyndns.org/");

System.out.println(fetcher.getMyExternalIpAddress());
}
}

㈣ java怎么解析Wireshark抓包文件

注意:

  1. 如果链接另一个Web站点时,客户端将再次对下一个站点进行DNS查询(156、157帧),TCP三次握手(158、159、160帧)。

  2. 31帧是客户端请求网络,通过DNS服务器解析IP地址的过程。标识为“A”记录。

  3. 32帧是DNS服务器回应客户端请求的过程。标识为response。

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

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

代码如下;

/*******************
*JpcapTip.java
*/
packagem;

importjpcap.PacketReceiver;
importjpcap.JpcapCaptor;
importjpcap.packet.*;
importjpcap.NetworkInterface;
importjpcap.NetworkInterfaceAddress;
//importjava.net.InetAddress;
//importjava.net.UnknownHostException;

{

publicvoidreceivePacket(Packetpacket){
System.out.println("********************************************");
/*IP数据报报文头*/
byte[]l=packet.header;
/*
for(intt=0;t<21;t++){
System.out.print(l[t]+"***");
}
*/
Stringstr="";
System.out.print("报文头:");
for(inti=0;i<l.length;i++){
//str=str+l;
intm=0;
m=l[i];
m=m<<24;
m=m>>>24;
str=str+Integer.toHexString(m);
//System.out.print("***"+l[i]);
}
System.out.println(str);
intd=l.length;
System.out.println("首部长度:"+(d*8)+"bit");

/*分析源IP地址和目的IP地址*/
/*分析协议类型*/
/**
if(packet.getClass().equals(IPPacket.class)){
IPPacketipPacket=(IPPacket)packet;
byte[]iph=ipPacket.option;
Stringiphstr=newString(iph);
System.out.println(iphstr);
}
*/
if(packet.getClass().equals(ARPPacket.class))
{
System.out.println("协议类型:ARP协议");
try{
ARPPacketarpPacket=(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(Exceptione){
e.printStackTrace();
}
}
else
if(packet.getClass().equals(UDPPacket.class))
{
System.out.println("协议类型:UDP协议");
try{
UDPPacketudpPacket=(UDPPacket)packet;
System.out.println("源IP地址为:"+udpPacket.src_ip);
inttport=udpPacket.src_port;
System.out.println("源端口为:"+tport);
System.out.println("目的IP地址为:"+udpPacket.dst_ip);
intlport=udpPacket.dst_port;
System.out.println("目的端口为:"+lport);
}catch(Exceptione){
e.printStackTrace();
}
}
else
if(packet.getClass().equals(TCPPacket.class)){
System.out.println("协议类型:TCP协议");
try{
TCPPackettcpPacket=(TCPPacket)packet;
inttport=tcpPacket.src_port;
System.out.println("源IP地址为:"+tcpPacket.src_ip);
System.out.println("源端口为:"+tport);
System.out.println("目的IP地址为:"+tcpPacket.dst_ip);
intlport=tcpPacket.dst_port;
System.out.println("目的端口为:"+lport);
}catch(Exceptione){
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;
Stringstr1="";
System.out.print("数据:");
for(inti=0;i<k.length;i++){
//intm=0;
//m=k[i];
//m=m<<24;
//m=m>>>24;
//str1=str+Integer.toHexString(m);
str1=newString(k);
//str1=str1+k[i];
//System.out.print("***"+k[i]);
}
System.out.println(str1);
System.out.println("数据报类型:"+packet.getClass());

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

publicstaticvoidmain(String[]args)throwsException{
//TODO自动生成方法存根

NetworkInterface[]devices=JpcapCaptor.getDeviceList();//.getDeviceList();.
//for(inti=0;i<devices.length;i++){
inta=0;
//try{
/*本地网络信息*/
byte[]b=devices[1].mac_address;//网卡物理地址
//}
//catch(){}
System.out.print("网卡MAC:00");
for(intj=0;j<b.length;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(intn=0;n<k.length;n++){
System.out.println("本机IP地址:"+k[n].address);//本机IP地址
System.out.println("子网掩码:"+k[n].subnet);//子网掩码
}
System.out.println("网络连接类型:"+devices[1].datalink_description);
//}
NetworkInterfacedeviceName=devices[1];
/*将网卡设为混杂模式下用网络设备deviceName*/
JpcapCaptorjpcap=JpcapCaptor.openDevice(deviceName,2000,false,1);//openDevice(deviceName,1028,false,1);
jpcap.loopPacket(-1,newJpcapTip());
}

}
阅读全文

与java解析ip数据包相关的资料

热点内容
remove命令linux 浏览:599
php发送邮件链接 浏览:34
创维冰箱压缩机 浏览:869
nginxopenssl交叉编译 浏览:750
相机卡无法创建新文件夹 浏览:225
单片机照明控制系统程序代码 浏览:10
服务编程一体化 浏览:471
tx小霸王服务器是什么意思 浏览:545
计算机编程工龄工资怎么算 浏览:491
macandroid配置环境变量 浏览:854
做项目文件夹的图标 浏览:327
数控车床车轴编程教程 浏览:728
怎么解压截图软件 浏览:885
算法符号椭圆 浏览:174
网络蚂蚁app是什么 浏览:273
php面向对象编程开发 浏览:798
唱吧如何解绑其他app 浏览:318
程序员去工厂好吗 浏览:497
阿里云租服务器企业要实名认证吗 浏览:928
mfc图形怎么输入命令 浏览:653