title: Client-Server UDP connection
body: <p>I am trying to send Data from the server to client 1 bit at a time. Client should respond back with an ACK whenever it received a bit from the server. </p>
  
  <p>What is currently happening is that when client sends the initial requests to the server, data is getting passed to the server. When server is sending the required data to the client, the data is getting looped back to itself and client is left in an indefinite waiting hang. </p>
  
  <p>I've attached the code below for client and server. Please have a look at it and advise on where I am going wrong.</p>
  
  <p>CLIENT SIDE:</p>
  
  <pre><code>import java.io.IOException;
  import java.net.*;           
  import java.util.Scanner;
  
  class UDPClient extends Thread implements Runnable                                                                              
  {                 
  DatagramSocket clientSocket;
  InetAddress IPAddress;
  public static void main(String args[]) throws Exception                                       
  {                                 
      try
      {
          UDPClient t1 = new UDPClient();
          Thread t = new Thread(t1);
          t.start();
      }
      catch (Exception e)
      {
          System.out.println("Error!");
      }
  }
  
  public UDPClient() throws Exception
  {
      this.clientSocket = new DatagramSocket();
      this.IPAddress = InetAddress.getByName("Localhost");
      clientSocket.setSoTimeout(5000);
  }
  public void run()
  {                                                                    
      byte[] sendData = new byte[1024];                                                          
      byte[] receiveData = new byte[1024];
      String m ="1001";
      String checksum = "1111";
      String checksumSend = "";
      String sentence="";
      String sentence1="";
      String dataRequired="";
      try
      {
          dataRequired = dataRequired.concat(m+":Data");
          sendData = dataRequired.getBytes();
          DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
          clientSocket.send(sendPacket);
          checksumSend = checksumSend.concat(checksum+":checksum");
          sendData = checksumSend.getBytes();
          DatagramPacket sendPacket2 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
          clientSocket.send(sendPacket2);
          Scanner in = new Scanner(System.in);
          System.out.println("Enter the Window size: ");
          int s = in.nextInt();
          String WinSize="";
          WinSize = WinSize.concat(s+":Windowsize");
          sendData = WinSize.getBytes();
          DatagramPacket sendPacket3 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
          clientSocket.send(sendPacket3);
          String finished="Finished";
          sendData = finished.getBytes();
          DatagramPacket sendPacket6 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
          clientSocket.send(sendPacket6);
          do
          {
              try
              {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  System.out.println("I am getting executed");
                  clientSocket.receive(receivePacket);  //After this step, nothing gets executed
                  sentence = new String(receivePacket.getData(),0,receivePacket.getLength()); 
                  System.out.println("Received from Server: " + sentence);
                  if(receivePacket != null)
                      sentence1 = "ACK";
                  sendData = sentence1.getBytes();
                  DatagramPacket sendPacket4 =
                          new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
                  clientSocket.send(sendPacket4);
              }
              catch (SocketTimeoutException a)
              {
                  System.out.println("Timed out!");
              }
          }while(sentence!=null); 
      }
      catch (IOException e) 
      {
          System.err.println(e);
      }
      finally
      {
          clientSocket.close(); 
      }
  }
  }   
  </code></pre>
  
  <p>SERVER SIDE:</p>
  
  <pre><code>import java.io.IOException;
  import java.net.*;
  
  public class UDPServer extends Thread implements Runnable
  {
  DatagramSocket serverSocket;
  public static void main(String args[]) throws Exception
  {
      try
      {
          UDPServer t1 = new UDPServer();
          Thread t = new Thread(t1);
          t.start();
      }
      catch (Exception e)
      {
          System.out.println("Error!");
      }
  }
  
  public UDPServer() throws Exception
  {
      this.serverSocket = new DatagramSocket(9876);
  }
  public  void run()
  {
      byte[] receiveData = new byte[1024];
      byte[] sendData = new byte[1024];
      String checksum="";
      String Data = "";
      String WinSize = "";
      String carry = "0";
      String output="";
      String n,o;
      String output1;
      String sentence1="";
      int i,j=0;
      while(true)
      {
          String sentence="";
          try
          {
              DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
              serverSocket.receive(receivePacket);
              InetAddress IPAddress = receivePacket.getAddress();
              sentence = new String( receivePacket.getData(),0,receivePacket.getLength());
              if(sentence.contains("checksum"))
              {
                  int len = sentence.length();
                  for(i=0;i&lt;len;i++)
                  {
                      if(sentence.charAt(i)==':')
                      {
                          checksum = sentence.substring(0,i);
                      }       
                  }
                  System.out.println("Checksum as specified by client is: " + checksum);
              }
              else if(sentence.contains("Data"))
              {
                  int len = sentence.length();
                  for(i=0;i&lt;len;i++)
                  {
                      if(sentence.charAt(i)==':')
                      {
                          Data = sentence.substring(0,i);
                      }   
                  }
                  System.out.println("Data requested by client is: " + Data);
              }
              else if(sentence.contains("Windowsize"))
              {
                  int len = sentence.length();
                  for(i=0;i&lt;len;i++)
                  {
                      if(sentence.charAt(i)==':')
                      {
                          WinSize = sentence.substring(0,i);
                      }   
                  }
                  System.out.println("Window Size is: " + WinSize);
              }
              else if(sentence.contains("Finished"))
              {
                  output1 = checksumAdd(carry,Data,checksum);
                  output = reverse(output1);
                  System.out.println("Checksum Addition before complementing digits = "+output);
                  output = complement(output);
                  output = reverse(output);
                  System.out.println("Checksum Addition after complementing digits = "+output);
                  int WindowSize = Integer.parseInt(WinSize);
                  int strlen = Data.length();
                  do
                  {
                      for(i=j;i&lt;(WindowSize+j);i++)
                      {
                          if(i!=strlen)
                          {
                                  String send = "";
                                  n = Data.substring(i,i+1);
                                  System.out.println("Value of n is: "+n);
                                  send = send.concat(n+":");
                                  o = output.substring(i,i+1);
                                  System.out.println("Value of o is: "+o);
                                  send = send.concat(o);
                                  sendData = send.getBytes();
                                  DatagramPacket sendPacket1 = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
                                  serverSocket.send(sendPacket1);     
  
                          }           
                          else
                              break;
                      }
                      j+=WindowSize;
                      DatagramPacket receivePacket2 = new DatagramPacket(receiveData, receiveData.length);
                      serverSocket.receive(receivePacket2);
                      sentence1 = new String( receivePacket2.getData(),0,receivePacket2.getLength());
                      System.out.println("sentence 1 is: "+sentence1);  //To be removed. Used for testing purposes
                  }while(i!=strlen);
              }
          }
          catch (IOException e) 
          {
              System.err.println(e);
          }
      }
  }
  
  
  public static String complement(String output) {
      String temp="";
      int len = output.length();
      for(int i=len;i&gt;0;i--)
      {
          String t = output.substring(i-1,i);
          if(t.equals("0"))
              temp = temp.concat("1");
          else if(t.equals("1"))
              temp = temp.concat("0");
      }
      return temp;
  }
  
  public static String reverse(String output)
  {
      String temp="";
      int len = output.length();
      for(int i=len;i&gt;0;i--)
      {
          String t = output.substring(i-1,i);
          temp = temp.concat(t);
      }
      return temp;
  }
  
  public static String checksumAdd(String carry, String Data,String checksum)
  {
      int strlen = Data.length();
      int flag=0;
      String output="";
      String output2="";
      String n,che;
      String sum = null;
      for(int i=strlen;i&gt;0;i--)
      {
          n=Data.substring(i-1,i);
          che = checksum.substring(i-1,i);
          if(n.equals("0") &amp;&amp; che.equals("0") &amp;&amp; carry.equals("0"))
          {
              sum = "0";
              carry = "0";
          }
          else if(n.equals("0") &amp;&amp; che.equals("0") &amp;&amp; carry.equals("1"))
          {
              sum = "1";
              carry = "0";
          }
          else if(n.equals("0") &amp;&amp; che.equals("1") &amp;&amp; carry.equals("0"))
          {
              sum = "1";
              carry = "0";
          }
          else if(n.equals("0") &amp;&amp; che.equals("1") &amp;&amp; carry.equals("1"))
          {
              sum = "0";
              carry = "1";
          }
          else if(n.equals("1") &amp;&amp; che.equals("0") &amp;&amp; carry.equals("0"))
          {
              sum = "1";
              carry = "0";
          }
          else if(n.equals("1") &amp;&amp; che.equals("0") &amp;&amp; carry.equals("1"))
          {
              sum = "0";
              carry = "1";
          }
          else if(n.equals("1") &amp;&amp; che.equals("1") &amp;&amp; carry.equals("0"))
          {
              sum = "0";
              carry = "1";
          }
          else if(n.equals("1") &amp;&amp; che.equals("1") &amp;&amp; carry.equals("1"))
          {
              sum = "1";
              carry = "1";
          }
          output = output.concat(sum);
      }
      if(carry.equals("1"))           
      {
              n = output.substring(0,1);
              if(n.equals("0"))
              {
                  sum = "1";
                  carry = "0";
              }
              else if(n.equals("1"))
              {
                  sum = "0";
                  carry = "1";
              }
              output2 = output2.concat(sum);
              for(int i=strlen-1;i&gt;0;i--)
              {
                  n=Data.substring(i-1,i);
                  if(n.equals("0") &amp;&amp; carry.equals("0"))
                  {
                      sum = "0";
                      carry = "0";
                  }
                  else if(n.equals("0") &amp;&amp; carry.equals("1"))
                  {
                      sum = "1";
                      carry = "0";
                  }
                  else if(n.equals("1") &amp;&amp; carry.equals("0"))
                  {
                      sum = "1";
                      carry = "0";
                  }
                  else if(n.equals("1") &amp;&amp; carry.equals("1"))
                  {
                      sum = "0";
                      carry = "1";
                  }
                  output2 = output2.concat(sum);
              }
          flag = 1;
      }
      if (flag==1)
          return output2;
      return output;
  }
  }
  </code></pre>