Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.support.annotation.Nullable;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class Main {

    @Nullable
    public static String getLocalIPAddress() {
        try {
            for (Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces(); eni
                    .hasMoreElements();) {
                NetworkInterface ni = eni.nextElement();
                for (Enumeration<InetAddress> eia = ni.getInetAddresses(); eia.hasMoreElements();) {
                    InetAddress ia = eia.nextElement();
                    if (!ia.isLoopbackAddress() && (ia instanceof Inet4Address)) {
                        return ia.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }
}