title: Two-way Java chat using socket
body: <p>I tried to find something similar to help me with my code and I also tried to do it by myself but I got stuck on the end and I would really appreciate if someone would help me with that. </p>
  
  <p>So I have already working simple Java Client/Server code and what i want to do is to upgrade it so it can work both ways, right now after launching the server and connecting to it by the client everything what u write as a client will be shown on the server too and i want to upgrade it so what i write on the client will be shown on the server and what i write on the server will be shown on the client.</p>
  
  <p>myServer.java</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  public class myServer {
      public static void main (String[] args) {
          try {
             int i=0;
             ServerSocket ss = new ServerSocket(9000);
             System.out.println("Listening...");
             Socket s = ss.accept();
             System.out.println("Connection accepted");
             InputStream d1 = s.getInputStream();
  
  
              while ((char)i !='q')
          {    
              i = d1.read();
              System.out.print((char)i);
  
          }
             s.close();
             ss.close();
          } catch (Exception e) {System.out.println("Error"); } 
  
      }
  }
  </code></pre>
  
  <p>myClient.java</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  public class myClient {
  public static void main(String[] args) {
      try {
          int i=0;
          Socket s=new Socket("localhost",9000);
          OutputStream o = s.getOutputStream();
  
  
          while ((char)i !='q')
          {
              i = System.in.read();
              o.write((char)i);
  
          }
          s.close();      
      } catch(Exception e) {System.out.println("Error"); }
  
  
  
      }
  }
  </code></pre>
  
  <p>Thank You in advance. </p>