用java 编写一个可以实现IP地址查询功能的课程设计 10
要求实现用JAVA程序可以查询出本机的IP地址和局域网当中存在的其他机器IP地址,同时根据给出的网站域名也可以查询出主机IP地址。通过IP可以查询出所需要的信息,如机器名...
要求实现用JAVA程序可以查询出本机的IP地址和局域网当中存在的其他机器IP地址,同时根据给出的网站域名也可以查询出主机IP地址。通过IP可以查询出所需要的信息,如机器名等。小女子很急,急求各位大神的答案!
展开
1个回答
2014-05-07
展开全部
下面是获得本机IP地址的方法,跟你的程序捆绑起来,互相发送消息的时候直接将IP发送过去
private static String[] getAllLocalHostIP(){
List<String> res=new ArrayList<String>();
Enumeration netInterfaces;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
Enumeration nii=ni.getInetAddresses();
while(nii.hasMoreElements()){
ip = (InetAddress) nii.nextElement();
if (ip.getHostAddress().indexOf(":") == -1) {
res.add(ip.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return (String[])res.toArray(new String[0]);
}
这是个扫描局域网ip的windows解决方案,在unix系统下可能有问题
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class LanIP {
public ArrayList<String> getLanIPArrayList() {
ArrayList<String> arrayIP = null;
try {
InitSystem initSystem = null;
initSystem = new InitSystem();
Thread thread = new Thread(initSystem);
thread.start();
thread.join();
arrayIP = initSystem.getArrayIPUsed();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return arrayIP;
}
private class InitSystem implements Runnable {
private int firstIP = 2;// 查询的 IP 地址的最后一位起始点
private int lastIP = 255;// 查询的 IP 地址的最后一位结束点
private volatile ArrayList<Thread> arrayThread;// 子线程段
private final int MAXTHREADNUM = 30; // 最大同时进行的子线程数量
private int threadNumNow;// 当前正在进行的子线程数量
private volatile ArrayList<String> arrayIP;// 局域网查询所有可能的 IP 地址的结果集
private volatile ArrayList<String> arrayIPUsed;// 局域网查询已经使用的 IP 地址的结果集
private InitSystem(String ip) {
arrayIP = new ArrayList<String>();
arrayIPUsed = new ArrayList<String>();
arrayThread = new ArrayList<Thread>();
setIPAddressList(ip);
}
private InitSystem() throws UnknownHostException {
this(InetAddress.getLocalHost().getHostAddress());
}
private synchronized ArrayList<String> getArrayIPUsed() {
try {
while (arrayIP.size() > 0) {
Thread.sleep(300);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return arrayIPUsed;
}
private void setIPAddressList(String ip) {
// 根据这个 ip 地址查询它所在的局域网的所有可能 IP 地址的集合
int lastPointIndex = ip.lastIndexOf('.');
String stringIPHead = ip.substring(0, ++lastPointIndex);
String stringIP = null;
for (int i = firstIP; i <= lastIP; i++) {
stringIP = stringIPHead + i;
arrayIP.add(stringIP);
}
}
public void run() {
synchronized (this) {
try {
while (arrayIP.size() > 0) {
while (threadNumNow >= MAXTHREADNUM) {
for (Thread thread : arrayThread) {
if (!thread.getState().equals(
Thread.State.TERMINATED)) {
thread.join(5);
}
--threadNumNow;
}
arrayThread = new ArrayList<Thread>();
}
Thread thread = new Thread(new InnerClass(arrayIP
.remove(0)));
thread.start();
threadNumNow++;
arrayThread.add(thread);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private class InnerClass implements Runnable {
// 线程查询一个 IP 是否是可以连接的 是则加入到相应的 IP 数组
private String ip;
private InnerClass(String ip) {
this.ip = ip;
}
private boolean isUsedIPAddress(String ip) {
synchronized (this) {
// 判断这个 IP 地址在当前局域网中是否是可连接的 IP
Process process = null;
BufferedReader bufReader = null;
String bufReadLineString = null;
try {
process = Runtime.getRuntime().exec(
"ping " + ip + " -w 100 -n 1");
bufReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
for (int i = 0; i < 6 && bufReader != null; i++) {
bufReader.readLine();
}
bufReadLineString = bufReader.readLine();
if (bufReadLineString == null) {
process.destroy();
return false;
}
if (bufReadLineString.indexOf("timed out") > 0
|| bufReadLineString.length() < 17
|| bufReadLineString.indexOf("invalid") > 0) {
process.destroy();
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
process.destroy();
return true;
}
}
public void run() {
synchronized (this) {
if (isUsedIPAddress(ip)) {
arrayIPUsed.add(ip);
}
}
}
}
}
}
private static String[] getAllLocalHostIP(){
List<String> res=new ArrayList<String>();
Enumeration netInterfaces;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
Enumeration nii=ni.getInetAddresses();
while(nii.hasMoreElements()){
ip = (InetAddress) nii.nextElement();
if (ip.getHostAddress().indexOf(":") == -1) {
res.add(ip.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return (String[])res.toArray(new String[0]);
}
这是个扫描局域网ip的windows解决方案,在unix系统下可能有问题
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class LanIP {
public ArrayList<String> getLanIPArrayList() {
ArrayList<String> arrayIP = null;
try {
InitSystem initSystem = null;
initSystem = new InitSystem();
Thread thread = new Thread(initSystem);
thread.start();
thread.join();
arrayIP = initSystem.getArrayIPUsed();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return arrayIP;
}
private class InitSystem implements Runnable {
private int firstIP = 2;// 查询的 IP 地址的最后一位起始点
private int lastIP = 255;// 查询的 IP 地址的最后一位结束点
private volatile ArrayList<Thread> arrayThread;// 子线程段
private final int MAXTHREADNUM = 30; // 最大同时进行的子线程数量
private int threadNumNow;// 当前正在进行的子线程数量
private volatile ArrayList<String> arrayIP;// 局域网查询所有可能的 IP 地址的结果集
private volatile ArrayList<String> arrayIPUsed;// 局域网查询已经使用的 IP 地址的结果集
private InitSystem(String ip) {
arrayIP = new ArrayList<String>();
arrayIPUsed = new ArrayList<String>();
arrayThread = new ArrayList<Thread>();
setIPAddressList(ip);
}
private InitSystem() throws UnknownHostException {
this(InetAddress.getLocalHost().getHostAddress());
}
private synchronized ArrayList<String> getArrayIPUsed() {
try {
while (arrayIP.size() > 0) {
Thread.sleep(300);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return arrayIPUsed;
}
private void setIPAddressList(String ip) {
// 根据这个 ip 地址查询它所在的局域网的所有可能 IP 地址的集合
int lastPointIndex = ip.lastIndexOf('.');
String stringIPHead = ip.substring(0, ++lastPointIndex);
String stringIP = null;
for (int i = firstIP; i <= lastIP; i++) {
stringIP = stringIPHead + i;
arrayIP.add(stringIP);
}
}
public void run() {
synchronized (this) {
try {
while (arrayIP.size() > 0) {
while (threadNumNow >= MAXTHREADNUM) {
for (Thread thread : arrayThread) {
if (!thread.getState().equals(
Thread.State.TERMINATED)) {
thread.join(5);
}
--threadNumNow;
}
arrayThread = new ArrayList<Thread>();
}
Thread thread = new Thread(new InnerClass(arrayIP
.remove(0)));
thread.start();
threadNumNow++;
arrayThread.add(thread);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private class InnerClass implements Runnable {
// 线程查询一个 IP 是否是可以连接的 是则加入到相应的 IP 数组
private String ip;
private InnerClass(String ip) {
this.ip = ip;
}
private boolean isUsedIPAddress(String ip) {
synchronized (this) {
// 判断这个 IP 地址在当前局域网中是否是可连接的 IP
Process process = null;
BufferedReader bufReader = null;
String bufReadLineString = null;
try {
process = Runtime.getRuntime().exec(
"ping " + ip + " -w 100 -n 1");
bufReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
for (int i = 0; i < 6 && bufReader != null; i++) {
bufReader.readLine();
}
bufReadLineString = bufReader.readLine();
if (bufReadLineString == null) {
process.destroy();
return false;
}
if (bufReadLineString.indexOf("timed out") > 0
|| bufReadLineString.length() < 17
|| bufReadLineString.indexOf("invalid") > 0) {
process.destroy();
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
process.destroy();
return true;
}
}
public void run() {
synchronized (this) {
if (isUsedIPAddress(ip)) {
arrayIPUsed.add(ip);
}
}
}
}
}
}
追问
你好,为什么我把上面的扫描局域网ip的windows解决方案的代码写下来之后,运行的结果还是显示本机的IP地址,而我需要的是显示局域网当中存在的其他机器IP地址,求解答,谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家提供动态代理服务器服务的企业,旨在帮助用户更好地管理网络访问和安全。以下是一些关于StormProxies的IP动态代理服务的特点:1. 高匿名性:StormProxies的动态代理服务器具有高匿名性,可以有效...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询