您的位置:首页 > 理论基础 > 计算机网络

Java网络编程:ip地址和端口

2017-03-21 16:01 225 查看

获取本机的ip地址

InetAddress 类: 用于标识网络上的硬件资源,表示互联网协议(ip)地址

InetAddress类没有公开的构造函数,需要通过类的一些静态方法获取类的实例。

package socket;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class TestSocket {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();

// 获取主机名和IP地址
System.out.println(address.getHostName());
System.out.println(address.getHostAddress());
// 得到主机名/ip地址
System.out.println(address);

// 获取字节数组形式的IP地址
// byte类型的范围是0-127,超过127则为负数;变为正数,加上256即可
byte[] bytes = address.getAddress();
System.out.println(Arrays.toString(bytes));

// 通过机器名获取InetAddress实例
// InetAddress address2 = InetAddress.getByName("www.baidu.com");

// 参数也可以为IP地址
// 注:如果ip地址不存在,或DNS服务器不允许进行ip地址和域名的映射,则返回该ip地址
InetAddress address2 = InetAddress.getByName("119.75.218.70");
System.out.println(address2.getHostAddress());

} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}


使用ping

借助Runtime.getRuntime().exec() 可以运行一个Windows的exe程序。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestSocket {
public static void main(String[] args) throws IOException {
Process p = Runtime.getRuntime().exec("ping " + "219.216.87.100" );
// 字符缓存流/字节流转字符流/字节流(Process对象的标准输出)
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine()) != null){
if(line.length() != 0)
sb.append(line + "\r\n");
}
System.out.println("本次指令返回的消息是: ");
System.out.println(sb.toString());
}
}


使用Java自带线程池判断本网段有多少可用的ip地址

package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class TestSocket {
public static void main(String[] args) throws IOException, InterruptedException {

InetAddress host = InetAddress.getLocalHost();
String ip = host.getHostAddress();
String ipRange = ip.substring(0, ip.lastIndexOf("."));
System.out.println("本机ip地址: " + ip);
System.out.println("网段是:" + ipRange);

final List<String> ips = Collections.synchronizedList(new ArrayList<String>());
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 15, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());
final AtomicInteger number = new AtomicInteger();
for (int i = 0; i < 255; i++) {
final String testIP = ipRange + "." + (i + 1);
threadPool.execute(new Runnable() {

@Override
public void run() {
boolean reachable = isReachable(testIP);
if (reachable)
ips.add(testIP);
synchronized(number){
System.out.println("已经完成:"+ number.incrementAndGet() + "个 ip 测试");
}
}
});
}
// 所有线程结束,关闭线程池
threadPool.shutdown();
// 等待线程池关闭,但是最多等待1个小时
if(threadPool.awaitTermination(1, TimeUnit.HOURS)){
System.out.println("如下ip地址可以连接");
for(String theip : ips){
System.out.println("总共有:" + ips.size() + " 个地址");
}
}
}

private static boolean isReachable(String ip) {
boolean reachable = false;
try {
Process p = Runtime.getRuntime().exec("ping " + ip);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine()) != null){
if(line.length() != 0){
sb.append(line + "\r\n");
}
}
// 当有time出现的时候,就表示连通了
reachable = sb.toString().contains("time<");
br.close();
return reachable;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: