title: Simple Client Server program
body: <p>I making an elementary project based on TCP/IP where server listens to the client and then provides a capital sentence of the incoming data.</p>
  
  <p>Server.java:</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  public class Server 
  {
  
      public static void main(String[] args) throws Exception
      {
          ServerSocket ss = new ServerSocket(7948);
          Socket s= ss.accept();
          System.out.print("Server connected\n");
  
          BufferedInputStream bis = new BufferedInputStream (s.getInputStream());
          BufferedOutputStream bos = new BufferedOutputStream (s.getOutputStream());
  
          while(true)
          {
              int a = bis.available();
              byte b[] = new byte[a];
              bis.read(b);
              String str = new String(b);
              str = str.toUpperCase();
              b = str.getBytes();
              bos.write(b,0,b.length);
              bos.flush();
  
              if(str.equals("BYE"))
                  break;
              else
                  continue;
          }
          System.out.print("\nServer Disconnecting");
          String str = "Adios Amigo";
          bos.write(str.getBytes());
          bos.flush();
  
          bis.close();
          bos.close();
          ss.close();
          s.close();
      }
  }
  </code></pre>
  
  <p>Client.java:</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  public class Client 
  {
  
      public static void main(String[] args) throws Exception
      {
          BufferedReader clientStream = new BufferedReader(new InputStreamReader(System.in));
          String str; 
          int a;
          byte[] b;
  
          Socket s = new Socket(InetAddress.getLocalHost(), 7948);
  
          BufferedOutputStream bos = new BufferedOutputStream (s.getOutputStream());
          BufferedInputStream bis = new BufferedInputStream (s.getInputStream());
  
          one:while(true)
          {
              str = clientStream.readLine();
              b =str.getBytes();
              bos.write(b);
              bos.flush();
  
              a=bis.available();
              b = new byte[a];
              bis.read(b);
              str = new String (b);
              str.trim();
              System.out.print("The server says: "+str);
              if (str.equals("BYE"))
              {
                  bis.read(b);
                  str = new String (b);
                  System.out.print("The server says: "+str);
                  break one;
              }
          }
  
          s.close();
          clientStream.close();
          bos.close();
          bis.close();
      }
  }
  </code></pre>
  
  <p>The program is working properly except for one problem, output at client side comes after two inputs. It means that I have to give two inputs from client sides to get the first output, and this continues. I am unable to track the bug.
  Can anyone please help ?</p>