get Port from SocketAddress - Java Network

Java examples for Network:Network Address

Description

get Port from SocketAddress

Demo Code


//package com.java2s;
import java.net.SocketAddress;

public class Main {
    public static Integer getPort(SocketAddress socketAddress) {
        if (socketAddress == null) {
            throw new NullPointerException("remoteAddres is null");
        }/*from w  ww.  jav  a2  s.com*/
        String address = socketAddress.toString();
        return Integer.valueOf(address.substring(address.indexOf(':') + 1));
    }
}

Related Tutorials