title: how to communicate between client and server in java
body: <p>I have a chat program. Now the code works for communicate between client and server via command line. But it gives an exception (java.net.SocketException: Socket is closed) while running. Please help me to fix that problem.</p>
  
  <p>In a java chat program,how will the communication be implemented between client and server?</p>
  
  <p>ie.</p>
  
  <p>client&lt;-->server (between server and client)</p>
  
  <pre><code>             or
  </code></pre>
  
  <p>client A&lt;-->server&lt;-->client B (server act as a bridge between two clients)</p>
  
  <p>Is the 2 way communication can be implemented through a single socket?
  Are there any other methods ? 
  How to communicate more than one client simultaneously? </p>
  
  <h2>server code</h2>
  
  <pre><code>class Server
  {
  ServerSocket server;
  Socket client;
  public Server()
  {
      try
      {
          server = new ServerSocket(2000);
          System.out.println("\tServer Started..........");
          while (true)
          {
              client = server.accept();
              Send objsend = new Send(client);
              Recive objrecive = new Recive(client);
              //client.close();
          }
      }
      catch (Exception e)
      {
          System.out.println("Exception4 " + e);
      }
  }
      public static void main(String arg[])
      {
          new Server();
      }
  }
  class Recive implements Runnable
  {
      Socket client;
      public Recive(Socket client1)
      {
          client=client1;
          Thread trsend=new Thread(this);
          trsend.start();
      }
      public void run()
      {
          ObjectInputStream ois;
          Message M=new Message();
          try
          {
              ois = new ObjectInputStream(client.getInputStream());
              M = (Message)ois.readObject();
              M.display();
              ois.close();
          }
          catch (Exception e)
          {
              System.out.println("Exception1 " + e);
          }
      }
  }    
  class Send implements Runnable
  {
      Socket client;
      public Send(Socket client1)
      {
          client=client1;
          Thread trrecive=new Thread(this);
          trrecive.start();
      }
      public void run()
      {
          Message M=new Message();
          InputStreamReader isr=new InputStreamReader(System.in);
          BufferedReader br=new BufferedReader(isr);
          try
          {
              System.out.println("Me(server)");
              M.strmessage=br.readLine();
              ObjectOutputStream oos=new ObjectOutputStream(cli    ent.getOutputStream());
              oos.writeObject((Message)M);
              oos.flush();
              oos.close();
          }
          catch (Exception e)
          {
              System.out.println("Exception " + e);
          }
      }
  }
  </code></pre>
  
  <h2>client code</h2>
  
  <pre><code>class Client
  {
  public static void main(String arg[])
  {
      try
      {
  
          Send objsend=new Send();
          Recive objrecive=new Recive();
      }
      catch(Exception e)
      {
          System.out.println("Exception "+e);
      }
  
  }
  }
  class Send implements Runnable
  {
  
  public Send()
  {
      Thread trsend=new Thread(this);
      trsend.start();
  }
  public void run()
  {
  
      try
      {
          Message M=new Message();
          InputStreamReader isr=new InputStreamReader(System.in);
          BufferedReader br=new BufferedReader(isr);
          while(true)
          {
              System.out.println("Me(client)");
              M.strmessage=br.readLine();
  
              Socket client=new Socket("localhost",2000);
              ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
              oos.writeObject((Message)M);
  
              oos.flush();
              oos.close();
          }
      }
      catch(Exception e)
      {
          System.out.println("Exception "+e);
      }
  }
  }
  class Recive implements Runnable
  {
  public Recive()
  {
      Thread trrecive=new Thread(this);
      trrecive.start();
  }
  public void run()
  {
  
      try
      {
          while(true)
          {
          Socket client=new Socket("localhost",2000);
          ObjectInputStream ois=new ObjectInputStream(client.getInputStream());
          Message CNE=(Message)ois.readObject();
          CNE.display();
  
          ois.close();
          }
  
      }
      catch(Exception e)
      {
          System.out.println("Exception "+e);
      }
  }
  }
  </code></pre>