集册 Java实例教程 从SocketAddress获取IP

从SocketAddress获取IP

欢马劈雪     最近更新时间:2020-01-02 10:19:05

1412
从SocketAddress获取IP


//package com.nowjava;/**来自 时代Java - nowjava.com**/

import java.net.SocketAddress;


public class Main {

    public static String getIP(SocketAddress socketAddress) {

        if (socketAddress == null) {

            throw new NullPointerException("remoteAddres is null");

        }

        String address = socketAddress.toString();

        return address.substring(1, address.indexOf(':'));

    }

}