MULTITHREADING

Mainserver.java:

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */


/**

 *

 * @author staffpc

 */

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */


/**

 *

 * @author rttd

 */

import java.io.*;

import java.net.*;


public class MainServer {

   public static void main(String[] args) throws Exception

   {

       Socket s=null;

       ServerSocket ss2=null;

       System.out.println("Server Listening ......");

       ss2=new ServerSocket(4446);

       while(true)

       {

           try

           {

               s=ss2.accept();

               System.out.println("connection Established");

               ServerThread st=new ServerThread(s);

               st.start();

           }

           catch(Exception e)

           {

               System.out.println("Connection Error!");

           }

       }

   }

}

class ServerThread extends Thread

{

    String line=null;

    DataInputStream is=null;

    DataInputStream br=null;

    PrintWriter cs=null;

    Socket s1=null;

    public ServerThread(Socket s)

    {

        s1=s;

    }

    public void run()

    {

        try

        {

            is=new DataInputStream(s1.getInputStream());

            cs=new PrintWriter(s1.getOutputStream());

            line=is.readLine();

            while(line.compareTo("QUIT")!=0)

            {

                cs.println(line);

                cs.flush();

                System.out.println("Response of Client: "+line);

                line=is.readLine();

            }

            is.close();

            cs.close();

            s1.close();

        }

        catch(Exception e)

        {

            

        }

    }

}

Client.java:

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */


/**

 *

 * @author staffpc

 */

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */


/**

 *

 * @author rttd

 */

import java.io.*;

import java.net.*;


public class Client1 {

public static void main(String[] args) throws IOException

{

    Socket s1=null;

    String line=null;

    DataInputStream br=null;

    DataInputStream is=null;

    PrintWriter cs=null;

    try

    {

        s1=new Socket("localhost",4446);

        br=new DataInputStream(System.in);

        is=new DataInputStream(s1.getInputStream());

        cs=new PrintWriter(s1.getOutputStream());

        

    }catch(IOException e)

    {

        System.out.println("IO Execution");

    }

    System.out.println("enter data to Echo Server (Enter Quit to ESC)");

    String response=null;

    try

    {

        line=br.readLine();

        while(line.compareTo("QUIT")!=0)

        {

            cs.println(line);

            cs.flush();

            response=is.readLine();

            System.out.println("Server Response "+ response);

            line=br.readLine();

        }

        is.close();

        cs.close();

        br.close();

        s1.close();

        System.out.println("conection closed");

        

    }

    catch(Exception e)

    {

        System.out.println("Socket read error!");

    }

}

}

Output:




Comments