Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to read information from Server Socket through TCP Client Socket ?.

Program to read information from Server Socket through TCP Client Socket

package com.hubberspot.networking.example;

import java.net.*; 
import java.io.*;

public class MyServerSocket
{
  public static void main(String args[]) 
 throws UnknownHostException, IOException
  {
    ServerSocket ss=new ServerSocket(4444);
    System.out.println("Server Started");
    Socket s = ss.accept(); 

  }
}


package com.hubberspot.networking.example;

import java.net.*; 
import java.io.*;

public class MyClientSocket
{
  public static void main(String args[]) 
  { 
    try{
      InetAddress addr = InetAddress.getByName("localhost");
      Socket s=new Socket (addr, 4444);
      System.out.println (s.getInetAddress());
      System.out.println (s.getPort());
      System.out.println (s.getLocalPort());
      s.close();
    }
     catch(Exception e)
       { e.printStackTrace(); }
  }
}


First compile both files. Run the server and run the client program.

Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot