java怎么获取本地ipv6地址

lyfadd85 2009-04-16 02:26:49
// dataConnection方法
// 构造与服务器交换数据用的socket
// 再用 PORT 命令将端口号通知给服务器
public Socket dataConnection(String ctrlcmd)
{
String cmd = "PORT " ; //存贮发送PORT命令的数据 所用的变量
int i ;
Socket dataSocket = null ;
try{
// 取得自己用的地址
byte[] address = InetAddress.getLocalHost().getAddress() ;
// 用适当的端口号构造服务器 Socket
ServerSocket serverDataSocket = new ServerSocket(0,1) ;
// 准备传送port 命令用的数据
for(i = 0; i < 4; ++i)
cmd = cmd + (address[i] & 0xff) + "," ;
cmd = cmd + (((serverDataSocket.getLocalPort()) / 256) & 0xff)
+ ","
+ (serverDataSocket.getLocalPort() & 0xff) ;
// 利用控制用的流发送PORT命令
ctrlOutput.println(cmd) ;
ctrlOutput.flush() ;
// 向服务器发送处理对象命令(LIST,RETR及STOR)
ctrlOutput.println(ctrlcmd) ;
ctrlOutput.flush() ;
// 受理服务器的连接
dataSocket = serverDataSocket.accept() ;
serverDataSocket.close() ;
}catch(Exception e)
{
e.printStackTrace();
//System.exit(1);
}
return dataSocket ;
}
上面的InetAddress.getLocalHost().getAddress() 是获取的ipv4的地址,如何使用Inet6Address获取IPV6地址
...全文
1187 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanlcn 2010-10-18
  • 打赏
  • 举报
回复
public static String getLocalIPv6Address() throws IOException {
InetAddress inetAddress = null;
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
Enumeration<InetAddress> inetAds = networkInterfaces.nextElement().getInetAddresses();
while (inetAds.hasMoreElements()) {
inetAddress = inetAds.nextElement();
//Check if it's ipv6 address and reserved address
if (inetAddress instanceof Inet6Address )
{
System.out.println(inetAddress);
}
}
}
如一宝宝 2009-04-16
  • 打赏
  • 举报
回复
有一个傻瓜的方法,在java中执行dos命令[ipconfig],可以得到ip6的地址!



package fx.toolkit;

import java.io.*;

/**
* java.lang.Runtime 每个 Java 应用程序都有一个 Runtime 类实例 ,使应用程序能够与其运行的环境相连接。
*
* 可以通过getRuntime 方法获取当前运行时, 应用程序不能创建自己的 Runtime 类实例
*
* 通过Process可以控制该子进程的执行或获取该子进程的信息。第二条语句的目的等待子进程完成再往下执行
*/
public class ExecDOS {

/**
* 执行DOS的内部命令
*
* @param command
* 一般应用程序或系统的cmd命令
*
*/
public void execDOS(String command) {
try {
Runtime.getRuntime().exec("cmd /c " + command);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}

/**
* 执行一个有标准输出的DOS可执行程序
*
* @param command
* 具有输出的cmd命名
*/
public static String execDOStoOutPut(String command) {
String result = "";
InputStream is = null;
InputStreamReader ir = null;
BufferedReader bufferedReader = null;
try {
Process process = Runtime.getRuntime().exec("cmd /c " + command);
// 创建一个使用默认大小输入缓冲区的缓冲字符输入流
is = process.getInputStream();
ir = new InputStreamReader(is);
bufferedReader = new BufferedReader(ir);
String temp;
while ((temp = bufferedReader.readLine()) != null) {
result += temp;
System.out.println(temp);
}
process.waitFor();
} catch (InterruptedException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
try {
bufferedReader.close();
ir.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}

public static void main(String[] args) {
ExecDOS.execDOStoOutPut("systeminfo");
}
}

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧