InetAddress ip=InetAddress.getLocalHost();
String sip=ip.getHostAddress();
这个语句在Windows下可以获得172.16.*.*的正常IP地址,在Linux下只能到127.0.0.1的回路IP
小弟正在开发局域网跨平台通信系统,C/S模型,要向MySQL中注入每个与之联系的Client端的IP信息,Windows下几个客户端已经可以完成任务,测试到Linux时发生问题,不知如何解决

解决方案 »

  1.   

    //定制Linux的IP,网关的系统命令
    String strcmd="route";
    //调用该系统命令
    java.lang.Process jprces = java.lang.Runtime.exec(strcmd);
    //得到该命令的标准输出流
    java.io.InputStream in = jprces.getInputStream();
      

  2.   


    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.util.Enumeration;/*********************************************************************
     * TestInetAddress.java  2007-12-10
     *
     * Copyright @ 2007 Inventec, Inc. All rights reserved.
     * Inventec PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     *********************************************************************//**
     * @Author: Jacky.fang
     * @Date: 2007-12-10 上午09:21:51
     * @Company: Inventec(Shanghai)Service Co. Ltd.
     */public class TestInetAddress {
    public static void main(String[] args) {
    Enumeration<NetworkInterface> netInterfaces = null;
    try {
    netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = netInterfaces.nextElement();
    System.out.println("DisplayName:" + ni.getDisplayName());
    System.out.println("Name:" + ni.getName()); Enumeration<InetAddress> ips = ni.getInetAddresses();
    while (ips.hasMoreElements()) {
    System.out.println("IP:"
    + ips.nextElement().getHostAddress());
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public static void main2(String[] args) {
    Enumeration netInterfaces = null;
    try {
    netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces
    .nextElement();
    System.out.println("DisplayName:" + ni.getDisplayName());
    System.out.println("Name:" + ni.getName()); Enumeration ips = ni.getInetAddresses();
    while (ips.hasMoreElements()) {
    System.out.println("IP:"
    + ((InetAddress) ips.nextElement())
    .getHostAddress());
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }}