㈠ 請問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抓包文件
Wireshark數據包解析:在Wireshark中關於數據包的叫法有三個術語,分別是幀、包、段。
注意:
如果鏈接另一個Web站點時,客戶端將再次對下一個站點進行DNS查詢(156、157幀),TCP三次握手(158、159、160幀)。
31幀是客戶端請求網路,通過DNS伺服器解析IP地址的過程。標識為「A」記錄。
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());
}
}