Java Local Address Get getLocalAddress()

Here you can find the source of getLocalAddress()

Description

get Local Address

License

Open Source License

Declaration

public static byte[] getLocalAddress() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern pattern = Pattern.compile("\\.", Pattern.CASE_INSENSITIVE);
    private static byte[] localAddress;

    public static byte[] getLocalAddress() {
        if (localAddress == null) {
            try {
                InetAddress inetAddress = InetAddress.getLocalHost();

                localAddress = inetAddress.getAddress();
            } catch (Exception e) {
            }//from  ww  w  . j ava2 s .c  o  m
        }
        return localAddress;
    }

    public static byte[] getAddress(String addr) {
        String[] array = pattern.split(addr);

        if (array.length != 4) {
            return new byte[] { 0x00, 0x00, 0x00, 0x00 };
        }

        byte[] bytes = new byte[4];

        for (int i = 0; i < array.length; i++) {
            try {
                bytes[i] = (byte) Integer.parseInt(array[i]);
            } catch (Exception e) {
                return new byte[] { 0x00, 0x00, 0x00, 0x00 };
            }
        }

        return bytes;
    }
}

Related

  1. getLocalAddress()
  2. getLocalAddress()
  3. getLocalAddress()
  4. getLocalAddress()
  5. getLocalAddress()
  6. getLocalAddress()
  7. getLocalAddress()
  8. getLocalAddress()
  9. getLocalAddress()