Pin It

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 :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot