Representing a Socket Address - Java Network

Java examples for Network:IP Address

Introduction

Creating an InetSocketAddress Object

Demo Code

import java.net.InetSocketAddress;

public class Main {
  public static void main(String[] args) {
    InetSocketAddress addr1 = new InetSocketAddress("::1", 12889);
    printSocketAddress(addr1);//  www  .j a  va 2  s .co  m

    InetSocketAddress addr2 = InetSocketAddress.createUnresolved("::1", 12881);
    printSocketAddress(addr2);
  }

  public static void printSocketAddress(InetSocketAddress sAddr) {
    System.out.println("Socket Address: " + sAddr.getAddress());
    System.out.println("Socket Host Name: " + sAddr.getHostName());
    System.out.println("Socket Port: " + sAddr.getPort());
    System.out.println("isUnresolved(): " + sAddr.isUnresolved());
    System.out.println();
  }
}

Result


Related Tutorials