title: Issue with Java Socket Programming : Client/Server cannot accept/read output/input
body: <p>nbp.java</p>
  
  <pre><code>public class nbp {
  public static void main(String[] args) throws Exception {
      Socket clientSocket = new Socket("localhost",6789);
      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
      BufferedInputStream in = new BufferedInputStream(clientSocket.getInputStream());
  
      out.write("A");
      out.write("\r\n");
      out.flush();
      System.out.println("NBP SENDS GW A");
      char c;
      StringBuilder sb = new StringBuilder();
      while(in.available()&gt;0){
          c = (char) in.read();
          sb.append(c);
      }
      System.out.println("NBP RECEIVES FROM GW " + sb.toString());
  
  }
  }
  </code></pre>
  
  <p>gw.Java</p>
  
  <pre><code>public class gw {
  public static void main(String[] args) throws Exception {
      ServerSocket listener = new ServerSocket(6789);
      Socket nbpSocket = listener.accept();
      Socket dspSocket = new Socket("localhost",6791);
      BufferedWriter nbpOut = new BufferedWriter(new OutputStreamWriter(nbpSocket.getOutputStream()));
      BufferedInputStream nbpIn = new BufferedInputStream(nbpSocket.getInputStream());
      BufferedWriter dspOut = new BufferedWriter(new OutputStreamWriter(dspSocket.getOutputStream()));
      BufferedInputStream dspIn = new BufferedInputStream(dspSocket.getInputStream());        
  
      char c;
      StringBuilder sb = new StringBuilder();
      while(nbpIn.available()&gt;0){
          c = (char) nbpIn.read();
          sb.append(c);
      }
      System.out.println("GW RECEIVES FROM NBP " + sb.toString());
  
      dspOut.write("B");
      dspOut.write("\r\n");
      dspOut.flush();
      System.out.println("GW SENDS DSP B");
  
      sb = new StringBuilder();
      while(dspIn.available()&gt;0){
          c = (char) dspIn.read();
          sb.append(c);
      }
      System.out.println("GW RECEIVES FROM DSP " + sb.toString());
  
      nbpOut.write(sb.toString());
      nbpOut.write("\r\n");
      nbpOut.flush();
      System.out.println("GW SENDS DSP " + sb.toString());
  }
  }
  </code></pre>
  
  <p>dsp.Java</p>
  
  <pre><code>public class dsp {
  public static void main(String[] args) throws Exception {
      ServerSocket listener = new ServerSocket(6791);
      Socket clientSocket = listener.accept();
      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
      BufferedInputStream in = new BufferedInputStream(clientSocket.getInputStream());
  
      char c;
      StringBuilder sb = new StringBuilder();
      while(in.available()&gt;0){
          c = (char) in.read();
          sb.append(c);
      }
      System.out.println("DSP RECEIVES FROM GW " + sb.toString());
  
      out.write("C");
      out.write("\r\n");
      out.flush();
              System.out.println("DSP SENDS GW C");
  
  }
  }
  </code></pre>
  
  <p>This is the above code I'm trying to run to understand Socket Programming in Java. So, I want to send a message from NBP to GW. Then I want GW to send a message to DSP and accept a reply from DSP. I then want to forward the message received by GW to NBP.</p>
  
  <p>Currently this is not happening as I planned it to. I would be grateful for any time and assistance provided. Thanks</p>
  
  <p>.P.S this is the output from each respective console</p>
  
  <p>nbp.console</p>
  
  <pre><code>NBP SENDS GW A
  NBP RECEIVES FROM GW 
  </code></pre>
  
  <p>gw.console</p>
  
  <pre><code>GW RECEIVES FROM NBP A
  GW SENDS DSP B
  GW RECEIVES FROM DSP 
  GW SENDS DSP 
  </code></pre>
  
  <p>dsp.console</p>
  
  <pre><code>DSP RECEIVES FROM GW 
  DSP SENDS GW C
  </code></pre>
  
  <p>as you can see.. gateway sends DSP 'B' but DSP cannot receive 'B'. It also cannot attempts to send 'C' to GW.. but GW does not accept 'C'.. GW also cannot send 'C' to NBP.. any idea why this happens ? sorry for the confusion.</p>