久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java實現(xiàn)局域網(wǎng)IP地址掃描

這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)局域網(wǎng)IP地址掃描,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

Java掃描局域網(wǎng)地址主要通過CMD命令,主要通過Runtime和Process類,由于同一局域網(wǎng)下的IP地址比較多需要通過Java的多線程來掃描端口。

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PingTask{
?? ?
?? ?private String ? address;
?? ?
?? ?public PingTask(String address){
?? ??? ?this.address=address;
?? ?}
?? ?
?? ?public PingResult run() {
?? ??? ?Runtime runtime;
?? ??? ?Process process;
?? ??? ?try {
?? ??? ??? ?runtime=Runtime.getRuntime();
?? ??? ??? ?process=runtime.exec("ping "+address);
?? ??? ??? ?BufferedInputStream inputStream=(BufferedInputStream) process.getInputStream();
?? ??? ??? ?byte [] bt =new byte[1024];
?? ??? ??? ?StringBuffer buffer=new StringBuffer();
?? ??? ??? ?int len=0;
?? ??? ??? ?while((len=inputStream.read(bt, 0,bt.length))!=-1){
?? ??? ??? ??? ?buffer.append(new String(bt, 0, len, "GBK"));
?? ??? ??? ?}
?? ??? ??? ?String regex="(\\d*)?";
?? ??? ??? ?String result="";
?? ??? ??? ?Pattern pattern=Pattern.compile(regex);
?? ??? ??? ?Matcher matcher=pattern.matcher(buffer.toString());
?? ??? ??? ?while(matcher.find()){
?? ??? ??? ??? ?if(!matcher.group().equals("")){
?? ??? ??? ??? ??? ?result=matcher.group();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?InetAddress inetAddress=InetAddress.getByName(address);
?? ??? ??? ?String hostname="";
?? ??? ??? ?if(!inetAddress.getHostName().equals(address)){
?? ??? ??? ??? ?hostname=inetAddress.getHostName();
?? ??? ??? ?}
?? ??? ??? ?String mac="";
?? ??? ??? ?process=runtime.exec("arp -a "+address);
?? ??? ??? ?
?? ??? ??? ?BufferedInputStream macinputStream=(BufferedInputStream) process.getInputStream();
?? ??? ??? ?byte [] macbt =new byte[1024];
?? ??? ??? ?StringBuffer macbuffer=new StringBuffer();
?? ??? ??? ?while((len=macinputStream.read(macbt, 0,macbt.length))!=-1){
?? ??? ??? ??? ?macbuffer.append(new String(macbt, 0, len, "GBK"));
?? ??? ??? ?}
?? ??? ??? ?String[] macresult=macbuffer.toString().trim().split("\r\n");
?? ??? ??? ?
?? ??? ??? ?if(!macbuffer.toString().contains("未找到 ARP")){
?? ??? ??? ??? ?mac=macresult[2].substring(20, 40).trim();
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?PingResult pingResult=new PingResult(address,(100-Integer.parseInt(result))+"%",mac,hostname);
?? ??? ??? ?
?? ??? ?} catch (UnsupportedEncodingException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?return pingResult;
?? ?}
?? ?
}

調(diào)用方法:

public static void main(String[] args) {
?? ??? ?PingResult result=new PingTask("123.123.123.123");
?? ??? ?System.out.println(result.toString());
?? ?}

PingResult 類

package com.xu.ip;

public class PingResult {
?? ?
?? ?private static String address;//IP地址
?? ?private static String result;//是否可以連接
?? ?private static String physicialaddress;//物理地址
?? ?private static String hostname;//主機(jī)名
?? ?
?? ?public String getPhysicialaddress() {
?? ??? ?return physicialaddress;
?? ?}

?? ?public void setPhysicialaddress(String physicialaddress) {
?? ??? ?PingResult.physicialaddress = physicialaddress;
?? ?}

?? ?public String getHostname() {
?? ??? ?return hostname;
?? ?}

?? ?public void setHostname(String hostname) {
?? ??? ?PingResult.hostname = hostname;
?? ?}

?? ?public PingResult(String address, String result) {
?? ??? ?PingResult.address = address;
?? ??? ?PingResult.result = result;
?? ?}

?? ?public PingResult() {
?? ??? ?
?? ?}
?? ?
?? ?public PingResult(String address, String result, String physicialaddress, String hostname) {
?? ??? ?PingResult.address = address;
?? ??? ?PingResult.result = result;
?? ??? ?PingResult.physicialaddress = physicialaddress;
?? ??? ?PingResult.hostname = hostname;
?? ?}

?? ?public String getAddress() {
?? ??? ?return address;
?? ?}
?? ?
?? ?public void setAddress(String address) {
?? ??? ?PingResult.address = address;
?? ?}
?? ?
?? ?public String getResult() {
?? ??? ?return result;
?? ?}
?? ?
?? ?public void setResult(String result) {
?? ??? ?PingResult.result = result;
?? ?}

?? ?@Override
?? ?public String toString() {
?? ??? ?return "PingResult [getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()="
?? ??? ??? ??? ?+ super.toString() + "]";
?? ?}
?? ?
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持html5模板網(wǎng)。

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)局域網(wǎng)聊天室功能,包括私聊、群聊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
主站蜘蛛池模板: 日韩在线视频免费观看 | 日韩欧美在线观看视频 | 久久久人成影片一区二区三区 | 日韩图区 | 亚洲精品一区二区三区在线 | 国产一区二区三区不卡av | 亚洲视频在线看 | 久久久精品| 日韩午夜精品 | 欧美一区二区三区在线观看视频 | 在线免费观看一区二区 | 影音先锋中文字幕在线观看 | 国产亚洲一区二区三区在线观看 | 成人欧美一区二区 | 国产在线精品一区二区 | 日韩午夜电影 | 国产区精品在线观看 | 国产情侣激情 | 亚洲激情综合 | 国产精品久久久久一区二区三区 | 欧美日韩精品区 | 中文字幕第7页 | 日韩欧美中文在线 | 欧美一区二区三区久久精品 | 久久久久亚洲精品国产 | 日韩一区精品 | 欧美日韩在线精品 | 欧美久久久久久久 | 91视频在线观看 | 免费观看羞羞视频网站 | 超碰美女在线 | 免费在线观看av | 免费看国产片在线观看 | 69电影网 | 午夜色播 | 中文字幕亚洲视频 | 国产欧美一区二区在线观看 | 性欧美精品一区二区三区在线播放 | 日韩无| 中文字幕视频三区 | 欧美精品1区 |