无法获取我的设备的IP地址

时间:2014-02-07 11:43:55

标签: android ip

我在获取设备的IP地址时遇到问题。

我想将它用作服务器并将其连接到另一个将充当客户端的Android设备。

以下是我为服务器应用编写的代码。

public class ServerActivity extends Activity {

private TextView serverStatus;

// DEFAULT IP
public static String SERVERIP = "10.0.2.15";

// DESIGNATE A PORT
public static final int SERVERPORT = 8080;

private Handler handler = new Handler();

private ServerSocket serverSocket;

private EditText serverMessage;

String str="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    serverStatus = (TextView) findViewById(R.id.TextView01);
    serverMessage = (EditText) findViewById(R.id.TextView02);

    SERVERIP = getLocalIpAddress();

    Thread fst = new Thread(new ServerThread());
    fst.start();
}


public class ServerThread implements Runnable {

    public void run() {
        try {
            if (SERVERIP != null) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Listening on IP: " + SERVERIP);
                    }
                });
                serverSocket = new ServerSocket(SERVERPORT);
                while (true) {
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            serverStatus.setText("Connected.");
                        }
                    });

                    try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                        String line = null;

                        while ((line = in.readLine()) != null) {
                            str = line;
                            Log.d("ServerActivity", line);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    serverMessage.setText(str);
                                }
                            });
                        }
                        break;
                    } catch (Exception e) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
                            }
                        });
                        e.printStackTrace();
                    }
                }
            } else {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        serverStatus.setText("Couldn't detect internet connection.");
                    }
                });
            }
        } catch (Exception e) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    serverStatus.setText("Error");
                }
            });
            e.printStackTrace();
        }
    }
}

private String getLocalIpAddress()
{
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        return "ERROR Obtaining IP";
    }
    return "No IP Available";   
}

@Override
protected void onStop() {
    super.onStop();
    try {
         serverSocket.close();
     } catch (IOException e) {
         e.printStackTrace();
     }
}

}

当我启动我的应用程序时,它显示在文本视图上:

"Listening on IP: fe80::2254:76ff:fe85:f2bd%wlan0".  

但是,我通过客户端连接另一个IP地址192.168.1.2(我在设备的设置中找到)。

我的问题是:如何在我的应用中获得第二个IP地址?

1 个答案:

答案 0 :(得分:1)

我希望您正在寻找IPv4地址;试试以下内容:

               boolean isIP4Address = inetAddress instanceof Inet4Address;
            if (!inetAddress.isLoopbackAddress() && isIP4Address) {
                String Ipv4     = inetAddress.getHostAddress();
                    //Ipv4 = 192.168.1.2    
                    }

这是Ipv6 LinkedLocal wifi IP fe80::2254:76ff:fe85:f2bd%wlan0