InetSocketAddress.InetSocketAddress(String, int) in Java

InetSocketAddress.InetSocketAddress(String, int): This method is available in the java.net.InetSocketAddress class of Java.

Syntax:

java.net.InetSocketAddress.InetSocketAddress(String hostname, int port)

This method takes two arguments. This method creates a socket address from a hostname and a port number.

An attempt will be made to resolve the hostname into an InetAddress.If that attempt fails, the address will be flagged as unresolved.

A valid port value is between 0 and 65535.A port number of zero will let the system pick up anephemeral port in a bind operation.

Parameters: Two parameters are required for this method.

hostname: the Host name.

port: The port number.

Throws:

1. IllegalArgumentException - if the port parameter is outside the range of valid port values, or if the hostname parameter is null.

2. SecurityException - if a security manager is present and permission to resolve the host name is denied.

Approach 1: When no exception

Java

package com.InetSocketAddress;

import java.net.InetSocketAddress;

public class InetSocketAddress3 {
    public static void main(String[] args) {

        int port = 8080;
        String hostname = "beingcodeexpert.blogspot.com";
        InetSocketAddress inetSocketAddress = new InetSocketAddress(hostname, port);

        System.out.println(inetSocketAddress);
    }
}

Output:

beingcodeexpert.blogspot.com/172.217.174.65:8080


Approach 2: IllegalArgumentException 

Java

package com.InetSocketAddress;

import java.net.InetSocketAddress;

public class InetSocketAddress3 {
    public static void main(String[] args) {

        int port = 98080;
        String hostname = "beingcodeexpert.blogspot.com";
        InetSocketAddress inetSocketAddress = new InetSocketAddress(hostname, port);

        System.out.println(inetSocketAddress);
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: port out of range:98080 at java.base/java.net.InetSocketAddress.checkPort(InetSocketAddress.java:153) at java.base/java.net.InetSocketAddress.<init>(InetSocketAddress.java:234) at com.InetSocketAddress.InetSocketAddress3.main(InetSocketAddress3.java:10)


Some other methods of InetSocketAddress class

InetSocketAddress.InetSocketAddress(int): This method creates a socket address where the IP address is the wildcard address and the port number a specified value.

InetSocketAddress.InetSocketAddress(InetAddress, int): This method creates a socket address from an IP address and a port number.

InetSocketAddress.InetSocketAddress(String, int): This method creates a socket address from a hostname and a port number.

InetSocketAddress.createUnresolved(String, int): This method creates an unresolved socket address from a hostname and a port number.

equals(Object): This method compares this object against the specified object. 

getAddress(): This method gets the InetAddress.

getHostName(): This method gets the hostname.

getHostString(): This method returns the hostname, or the String form of the address if it doesn't have a hostname (it was created using a literal).

getPort(): This method gets the port number.

hashCode(): This method returns a hashcode for this socket address.

isUnresolved(): This method checks whether the address has been resolved or not.

toString(): This method constructs a string representation of this InetSocketAddress.This String is constructed by calling toString() on the InetAddressand concatenating the port number (with a colon).

No comments:

Post a Comment