Advance Java

Learn New things from Rixosys

Client Socket Information

Client Socket Information

In this section, you will learn how to get client socket information. This is a very simple program of java network. Here, we have used the class named that implement the constructor of the Socket class passing two arguments as hostName and TIME_PORT. This program throws an IOException for the exception handling. This exception is thrown to indicate an I/O problem of some sort occurred.

Here, we are going to explore a method to retrieve the host name of the local system in a very simple way. In this way we find out the Local address, Local host information, reuseAddress, and address of the local system in a very simple manner.

Code Description

Here we are going to define about all the using method.

isBound(): This is a boolean type method. This method returns the binding state of the socket.
isConnected():  This method inform about the socket connectivity that is connected from server or may not.
getSoTimeout(): This method  returns the setting for So_TIMEOUT 0 returns implies that the option is disabled.
getSoLinger(): This is the method returns setting for SO_LINGER. -1 returns imply that the option is disabled.

Here is code of this program:


public class ClientSocketInformation{
  public static final short TIME_PORT = 135;
  public static void main(String[] args) throws IOException{
  String hostName= null;
  Socket socket= new Socket(hostName,TIME_PORT);
  System.out.println("Socket is =" + socket);
  System.out.println("is socket bound? ="+ socket.isBound());
  System.out.println("is socket Connected? ="+ socket.isConnected());
  System.out.println("show time out = "+ socket.getSoTimeout());
  System.out.println("so linger =" + socket.getSoLinger());
  System.out.println("TCP no Delay =" + socket.getTcpNoDelay());
  System.out.println("Traffic class =" + socket.getTrafficClass());
  System.out.println("channel is =" + socket.getChannel());
  System.out.println("Reuse address is =" + socket.getReuseAddress());
  System.out.println("close =" + socket.isClosed());
  InetAddress in= socket.getInetAddress();
  System.out.print("RAW IP Address - (byte[]) : ");
  byte[] b1 = in.getAddress();
  for (int i=0; i< b1.length; i++) {
  if (i > 0) {
  System.out.println("Is Loopback Address?  : " + in.isLoopbackAddress());
  System.out.println("Is Multicast Address? : " + in.isMulticastAddress());
  InetAddress address = socket.getLocalAddress();
  System.out.println("Local address =" + address);
  catch (UnknownHostException  e)  {
  catch (IOException e) {

Here Output of this  program:

C:\rixosys>java ClientSocketInformation

Socket is =Socket[addr=localhost/,port=135,localport=1329]
is socket bound? =true
is socket Connected? =true
show time out = 0
so linger =-1
TCP no Delay =false
Traffic class =0
channel is =null
Reuse address is =false
close =false

RAW IP Address - (byte[]) :
Is Loopback Address? : true
Is Multicast Address? : false

Local address =/