Saturday, 31 January 2015

JAVA program to find out ECHO Server using socket.

//  File name = ConServer.java

import java.net.*;
import java.io.*;
public class ConServer
{
    public static void main(String[] dha)
    {
        ServerSocket s=null;
        try
        {
            s=new ServerSocket(12345);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }       
        while(true)
        {
            try
            { 
          System.out.println("Wllcom To SERVER .......");          
                Socket s1=s.accept();                       
                System.out.println("Enter Your Message For Client ...............");           
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                OutputStream out=s1.getOutputStream();               
                BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(out));           
                String str;           
                System.out.println("Enter exit++ to stop");                       
                str=br.readLine();
                while(str!=null)
                {
                    if(str.equals("exit++"))
                        break;
                    bw.write(str);
                    bw.newLine();
                    System.out.println("Read: "+str);               
                    str=br.readLine();                               
                }                      
                br.close();
                bw.close();
                s1.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }       
        }
    }   
}

***********************************************************************

 // File Name = ConClient .java

import java.io.*;
import java.net.*;   
public class ConClient
{
    public static void main(String[] dha)
    {
        try
        {
            Socket s1=new Socket("127.0.0.1",12345);
            System.out.println("Wllcom To Client .......");
            System.out.println(" Message Is  .......");        
            InputStream is=s1.getInputStream();    
            BufferedReader br=new BufferedReader(new InputStreamReader(is));
            int i;
            while((i=br.read())!=-1)
            {               
                System.out.print((char)i);                                         
            }
                br.close();
                s1.close();        
    }//try over
        catch(IOException e)
        {           e.printStackTrace();
            System.out.println("Conncection Problem");
        }
    }// main over
  
}// class over



*******************************************************************************
******************** OUTPUT  ********************

ConServer.java



ConClient .java



***************************************************************************

How To Run Server-Client Program                       

***************************************************************************

Step 1 > javac ConServer.java
//compile file

Step 2 > start
//open new terminal

Step 3 > java ConServer

Step 4 >ConClient .java
//compile file In new terminal

Step 5 > java ConClient



No comments:

Post a Comment