Sunday, 28 June 2015

CLIENT-SERVER PROGRAM (SOCKET PROGRAMMING) :


Explanation :

In This Client server codes,Client has to send a number to server and server sends back an acknowledgment to client that a proper number is received. If client doesn't send a number then server gives an acknowledgment to client to send a number again.


READ-MUST :
1)IT IS MUST THAT SERVER NEEDS TO RUN FIRST THEN IT STARTS LISTENING.NOW RUN THE CLIENT CODE.
2)THIS WORKS FOR TWO SYSTEMS CONNECTED IN SAME NETWORK
(FOR EX. TWO SYSTEMS IN DIFFERENT BLOCKS(PLACES)) AND ALSO FOR ONE SYSTEM TOO PROVIDED LAN OR WIFI IS CONNECTED TO SYSTEM.
3)IF SYSTEM IS NOT CONNECTED TO ANY NETWORK THEN REPLACE THE CODE WITH LOCAL HOST CODE AT IP ADDRESS PLACE
  i.e. REPLACE THE STAR PART OF THE CODE WITH THE BELOW CODE


String host = "localhost";
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);



              CLIENT PROGRAM


import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class Client
{
 
 
        static Socket socket;
        static Boolean ack=true;
   public static void main(String args[])
   {
     
       try
       {
            while(ack)
          {
        
         //Enter port number less than 65536
         
           int port = 25001;
           
          / *********************************/  
           System.out.println("Enter ip address to connect : ");
           
//Scanner is used for Taking Input or giving output.

       /*Your system IP or Any other System's IP address both connected in same Network */
           
           Scanner sc = new Scanner(System.in);
           String ip = sc.nextLine();
             
             
           //A Socket is a combination of IP address and Port number.
           
           socket = new Socket(ip, port);
           
       /*******************************************/   
           
           System.out.println("Connected \n Enter a number to send to server");

           Scanner value=new Scanner(System.in);
           

           String number=value.nextLine();
         

  /*Print stream is used to send the information(number) to server*/
           PrintStream p=new PrintStream(socket.getOutputStream());
           p.println(number);
           
           
           System.out.println("Message sent to the server : "+number);
           
          
 /*scanner is used to receive the Information from Socket at Client*/
         
         Scanner s=new Scanner(socket.getInputStream());
           String number1=s.nextLine();
           
           System.out.println("Message received from the server : " +number1);
           
/*If a Number is not sent to Server then need to send it again.*/
            if(number1.contentEquals("Received Successfully"))
              ack=false;
           else
               ack=true;
          }
       }
     catch (Exception exception)
     {
         exception.printStackTrace();
     }
      
           try
           {
              socket.close();
           }
           catch(Exception e)
           {
               e.printStackTrace();
           }
       
   }
}





      SERVER PROGRAM


import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server
{

   private static Socket socket;

   public static void main(String[] args)
   {
       try
       {

           int port = 25001;
           
/*A system has so many ports,So server socket is used to listen at that port*/
           ServerSocket serverSocket = new ServerSocket(port);
           System.out.println("Server Started and listening to the port 25001");
           
           while(true)
           {
               //Reading the message from the client
               socket = serverSocket.accept();
              
   /*Here Socket is used to Receive the Information from Socket at Server*/
               


              Scanner s=new Scanner(socket.getInputStream());
               String number=s.nextLine();
               System.out.println("client : "+number);

               
               String returnMessage;
               
   //Here Checking whether The received message is a number or not
               try
               {
                   int numberInIntFormat = Integer.parseInt(number);
                   returnMessage="Received Successfully";
               }
               catch(NumberFormatException e)
               {
  /*Input was not a number. Sending proper message back to client.*/
                   returnMessage = "Please send a number\n";
               }

               //Sending the response back to the client.
               System.out.println("Message sent to the client is "+returnMessage);
               
        /*Print Stream is used to sent the Message to other system in network*/
              
             
        PrintStream p=new PrintStream(socket.getOutputStream());
               p.println(returnMessage);
           }
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }
       finally
       {
           try
           {
               socket.close();
           }
           catch(Exception e){}
       }
   }
}



No comments:

Post a Comment