title: Packet loss in socket programming java
body: <p>I am trying to send a file from client to server. Below is the code i have tried. But at times, there is a packet loss during the transfer. I am not sure where i am wrong.</p>
  
  <p>SERVER SIDE CODE:</p>
  
  <pre><code>public static void ReadAndWrite(byte[] aByte, Socket clientSocket,
              InputStream inputStream, String fileOutput)
                      throws FileNotFoundException, IOException {
          int bytesRead;
          FileOutputStream fileOutputStream = null;
          BufferedOutputStream bufferedOutputStream = null;
          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
          try
          {
          fileOutputStream = new FileOutputStream( fileOutput );
          bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
          bytesRead = inputStream.read(aByte, 0, aByte.length);
          System.out.println("The length is "+bytesRead);
          int count = 0;
          do {
              count++;
              byteArrayOutputStream.write(aByte);
              bytesRead = inputStream.read(aByte);
          } while (bytesRead != -1);
          System.out.println("The count is "+count);
          System.out.println("The length is "+byteArrayOutputStream.size());
          bufferedOutputStream.write(byteArrayOutputStream.toByteArray());
          bufferedOutputStream.flush();
          bufferedOutputStream.close();
          clientSocket.close();
          }
          catch(Exception ex)
          {
              Logger.writeLog(ex,Listen.class.getName(), LogType.EXCEPTION);  
              throw ex;
          }
  </code></pre>
  
  <p>CLIENT SIDE CODE:</p>
  
  <pre><code>public  void readByteArrayAndWriteToClientSocket(
              Socket connectionSocket, BufferedOutputStream outToClient, String fileToSend ) throws Exception
      {
          try{
          if (outToClient != null) 
          {
              File myFile = new File(fileToSend);
              System.out.println(myFile.length());
              byte[] byteArray = new byte[(int) myFile.length()];
  
              FileInputStream fileInputStream = null;
  
              try {
                  fileInputStream = new FileInputStream(myFile);
              } catch (IOException ex) {
                  Logger.writeLog(ex, FileUtility.class.getName(), LogType.EXCEPTION);
                  throw ex;
              }
              BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
  
              try {
                  bufferedInputStream.read(byteArray, 0, byteArray.length);
                  outToClient.write(byteArray, 0, byteArray.length);
                  outToClient.flush();
                  outToClient.close();
                  connectionSocket.close();
  
  
                  return;
              } catch (IOException ex) {
                  Logger.writeLog(ex, FileUtility.class.getName(), LogType.EXCEPTION);
                  throw ex;
              }
  
          }
          }catch (Exception e) {
              Logger.writeLog(e, getClass().getName(), LogType.EXCEPTION);
              throw e;
          }
      }
  </code></pre>