5/22/2012

socket programming java basic classic example

easywayprogramming.com Socket programming in java basic classic example

Here is the classic example of socket programming in java.
GreetingSever.java
import java.net.*;
import java.io.*;

public class GreetingServer extends Thread
{
       private ServerSocket serverSocket;
       Socket server;

       public GreetingServer(int port) throws IOException, SQLException, ClassNotFoundException, Exception
       {
          serverSocket = new ServerSocket(port);
          serverSocket.setSoTimeout(180000);
       }

       public void run()
       {
           while(true)
          {
               try
               {
                  server = serverSocket.accept();
                  DataInputStream din=new DataInputStream(server.getInputStream());
                  DataOutputStream dout=new DataOutputStream(server.getOutputStream());

                  dout.writeUTF("server: -i am greeting server");
                  dout.writeUTF("server:- hi! hello client");

                  System.out.println(din.readUTF());
                  System.out.println(din.readUTF());
              }
             catch(SocketTimeoutException st)
             {
                   System.out.println("Socket timed out!");
                  break;
             }
             catch(IOException e)
             {
                  e.printStackTrace();
                  break;
             }
             catch(Exception ex)
            {
                  System.out.println(ex);
            }
          }
       }
      
       public static void main(String [] args) throws IOException, SQLException, ClassNotFoundException, Exception
       {
              //int port = Integer.parseInt(args[0]);
              Thread t = new GreetingServer(6066);
              t.start();
       }
}

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

public class GreetingClient
{
   public static void main(String [] args)
   {
      String serverName = "localhost";
      int port = 6066;
      try
      {
         System.out.println("Connecting to " + serverName
                             + " on port " + port);
         Socket client = new Socket(serverName, port);

         System.out.println("Just connected to "
                      + client.getRemoteSocketAddress());

        DataInputStream in=new DataInputStream(client.getInputStream());
        System.out.println(in.readUTF());
        System.out.println(in.readUTF());

         DataOutputStream out =
                       new DataOutputStream(client.getOutPutStream());

         out.writeUTF("Hello from "
                      + client.getLocalSocketAddress());
         out.writeUTF("client: hello to server")

         client.close();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

**Important notes:
       in greeting client server-name should be "localhost" if client and server are running oh same machines.
       If server and client are on different machines then in client we write a server-name i.e. computer_name of machine on which server is running.
       port number defined on client & server should be same. that means client is connecting to port on server.
       port number on client and server should be greater than 1023 (port  > 1023), because ports 0 to 1023 are well known ports, are used for specific purpose.  

Now we see running process
1.  open command promt or jcreator.
     Compile GreetingServer.java file.
     If no error, then run GreetingServer class.

2. now open another command promt or jcreator
    compile GreetingClient.java file
    if no error, then run GreetingClient class.

see what happens!!!!!!!!!

click here: Socket programming in java theory

2 comments:

  1. I have visited this blog first time and i got a lot of informative data from here which is quiet helpful for me indeed. 
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  2. I really like your blog. You make it interesting to read and entertaining at the same time. I cant wait to read more from you.
    python Training in Pune
    python Training in Chennai
    python Training in Bangalore

    ReplyDelete