- 黑桃猪
-
用双网卡同时访问内外网暂时没有很完美的解决办法,因为存在路由冲突,毕竟有两个网关地址,现在可以试试下面的办法:
先来解决双网卡冲突的问题。可以通过改变路由地址表搞定。以你的单位用机为例,机器有两块网卡,接到两台路由器上:
内部网地址设置为192.168.1.110,子网掩码:255.255.255.0,网关:192.168.1.1
办公网地址:10.94.12.123,子网掩码:255.255.255.0,网关:10.94.12.254
如果按正常的设置方法设置每块网卡的IP地址和网关,再cmd下使用route print查看时会看到以0.0.0.0 0.0.0.0 开头的两个东西,即指向0.0.0.0的有两个网关,这样就会出现路由冲突,两个网络的访问存在困难。要实现同时访问两个网络就要用到route命令
第一步:route delete 0.0.0.0(删除所有0.0.0.0的路由)
第二步:route add 0.0.0.0 mask 0.0.0.0 192.168.1.1(添加0.0.0.0网络路由)
第三步:route add 10.0.0.0 mask 255.0.0.0 10.94.12.254(添加10.0.0.0网络路由)
这时就可以同时访问两个网络了,但碰到一个问题,使用上述命令添加的路由在系统重新启动后会自动丢失,保存现有的路由表
作一个BAT文件吧,把上面3步的内容全加进去,并设置系统在开始的启动菜单里运行他。这样只要一开机,路由表就会按我们所需要的进行变更,双网的访问再也不会存在问题了。
- CarieVinne
-
import java.io.*; import java.net.*; import java.util.*; import static java.lang.System.out; public class ListNets { public static void main(String args[]) throws SocketException { Enumeration nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netint : Collections.list(nets)) displayInterfaceInformation(netint); } static void displayInterfaceInformation(NetworkInterface netint) throws SocketException { out.printf("Display name: %s ", netint.getDisplayName()); out.printf("Name: %s ", netint.getName()); Enumeration inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { out.printf("InetAddress: %s ", inetAddress); } out.printf(" "); } } The following is sample output from the example program: Display name: bge0 Name: bge0 InetAddress: /fe80:0:0:0:203:baff:fef2:e99d%2 InetAddress: /121.153.225.59 Display name: lo0 Name: lo0 InetAddress: /0:0:0:0:0:0:0:1%1 InetAddress: /127.0.0.1
google 上搜索how to get ip when have two network card java
- 里论外几
-
package main;
import java.io.*;
import java.util.*;
public class ProcessBuilderShow{
public static List<String> getIPAddress(){
Process p = null;
//IP地址列表
List<String> address = new ArrayList<String>();
try{
//执行ipconfig /all命令
p = new ProcessBuilder("ipconfig", "/all").start();
}catch (IOException e){
return address;
}
byte[] b = new byte[56];
StringBuffer sb = new StringBuffer();
//读取进程输出值
InputStream in = p.getInputStream();
try{
while (in.read(b)>0){
sb.append(new String(b));
}
}catch (IOException e1){
}finally{
try{
in.close();
}catch (IOException e2){
}
}
//以下分析输出值,得到IP地址
String rtValue = sb.substring(0);
int i = rtValue.indexOf("IP Address. . . . . . . . . . . . :");
while(i>0){
rtValue = rtValue.substring(i + "IP Address. . . . . . . . . . . . :".length());
address.add(rtValue.substring(0,15));
i = rtValue.indexOf("IP Address. . . . . . . . . . . . :");
}
return address;
}
public static void main(String[] args){
List<String> address = ProcessBuilderShow.getIPAddress();
while(!address.isEmpty()){
System.out.println("IP地址:"+ address.get(0));
address.remove(0);
}
}
}
这个可以达到要求的目的,不过这种代码要求程序调用系统的默认命令,对于远程的查看还得根据服务开启状况和防火墙设置等分情况讨论了.
- ardim
-
Hello buddy,
This code should be solve u problem.
public static void main(String args[])
throws SocketException
{
Enumeration<NetworkInterface> nets =
NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface netint)
throws SocketException
{
System.out.println("Display name: "
+ netint.getDisplayName());
System.out.println("Hardware address: "
+ Arrays.toString(netint.getHardwareAddress()));
}
----------------------------
See as java.net.NetworkInterface api.