title: Software caused connection abort: socket write error
body: <p>Ok so, I've been looking all over the place and I see this is a common issue here and there and some people found a solution, others did not, unfortunately the solutions I found did not work, so I am wondering what is going on here.  </p>
  
  <p>The purpose of my programs is to either manually send or receive files or "sync" where the server will sync a folder with a folder on my computer.</p>
  
  <p>The following are my code:</p>
  
  <p>ClientConnection.java</p>
  
  <pre><code>public class ClientConnection implements Runnable {
  
  private Socket clientSocket;
  private BufferedReader in = null;
  static List&lt;String&gt; reqFile = new ArrayList&lt;String&gt;();
  static List&lt;String&gt; fname = new ArrayList&lt;String&gt;();
  static List&lt;String&gt; results = new ArrayList&lt;String&gt;();
  String folderPath="C:/Users/shami_000/Desktop/ServerFiles";
  
  public ClientConnection(Socket client) {
      this.clientSocket = client;
  }
  
  @Override
  public void run() {
      try {
          in = new BufferedReader(new InputStreamReader(
                  clientSocket.getInputStream()));
          String clientSelection;
          clientSelection = in.readLine();
          System.out.println(clientSelection);
          while ((clientSelection) != null) {
              switch (clientSelection) {
  
              case "1":
                  receiveFile();
                  break;
              case "2":
                  String outGoingFileName;
                  while ((outGoingFileName = in.readLine()) != null) {
                      sendFile(outGoingFileName);
                  }
  
                  break;
              case "3":
                  sync();
                  break;
              default:
                  System.out.println("Incorrect command received.");
                  break;
              }
              //in.close();
              break;
          }
  
      } catch (IOException ex) {
          Logger.getLogger(ClientConnection.class.getName()).log(
                  Level.SEVERE, null, ex);
      }
  }
  
  public void receiveFile() {
      try {
          int bytesRead;
  
          DataInputStream clientData = new DataInputStream(
                  clientSocket.getInputStream());
  
          String fileName = clientData.readUTF();
          OutputStream output = new FileOutputStream((fileName));
          long size = clientData.readLong();
          byte[] buffer = new byte[1024];
          while (size &gt; 0
                  &amp;&amp; (bytesRead = clientData.read(buffer, 0,
                          (int) Math.min(buffer.length, size))) != -1) {
              output.write(buffer, 0, bytesRead);
              size -= bytesRead;
          }
  
          output.close();
          clientData.close();
  
          System.out.println("File " + fileName + " received from client.");
      } catch (IOException ex) {
          System.err.println("Client error. Connection closed.");
      }
  }
  
  
  
  public void sync() {
      int bytesRead;
      try {
          DataInputStream clientData = new DataInputStream(
                  clientSocket.getInputStream());
  
          String fileNames = clientData.readUTF();
          //System.out.println(fileNames);
  
          File folder = new File("C:/Users/shami_000/Desktop/ServerFiles");
          File[] listOfFiles = folder.listFiles();
          String list = "";
  
          for (int i = 0; i &lt; listOfFiles.length; i++) {
              if (listOfFiles[i].isFile()) {
                  results.add(listOfFiles[i].getName());
              }
          }
          StringTokenizer st = new StringTokenizer(fileNames, ",");
          while (st.hasMoreTokens()) {
              String name = (String) st.nextElement();
              fname.add(name);
          }
          getReqFiles();
          for (int i = 0; i &lt; reqFile.size(); i++) {
              list = list + reqFile.get(i) + ",";
          }
  
          System.out.println(list);
          OutputStream os = clientSocket.getOutputStream();
  
          // Sending file name to the client
          DataOutputStream dos = new DataOutputStream(os);
          dos.writeUTF(list);
          dos.flush();
          //dos.close();
          os.close();
          clientData.close();
          reqFile.clear();
          fname.clear();
          results.clear();
  
      } catch (Exception e) {
          System.out.println(e);
      }
  }
  
  public static void getReqFiles(){
      boolean there = false;
      String file="";
      if(results.isEmpty())
          reqFile=fname;
      else{
          for(int i=0;i&lt;fname.size();i++){
              file=fname.get(i);
              if(!(results.contains(file)))
                  reqFile.add(file);
          }
      }
  
  }
  }
  </code></pre>
  
  <p>FileClient.java</p>
  
  <pre><code> public class FileClient {
  
  private static Socket sock;
  private static String fileName;
  private static BufferedReader stdin;
  private static PrintStream os;
  private static String path = "C:\\Users\\shami_000\\Desktop\\ClientFiles\\";
  
  public static void main(String[] args) throws IOException {
      try {
          sock = new Socket("localhost", 4444);
          stdin = new BufferedReader(new InputStreamReader(System.in));
      } catch (Exception e) {
          System.err
                  .println("Cannot connect to the server, try again later.");
          System.exit(1);
      }
  
      os = new PrintStream(sock.getOutputStream());
  
      try {
          switch (Integer.parseInt(selectAction())) {
          case 1:
              os.println("1");
              //sendFile();
              break;
          case 2:
              os.println("2");
              System.err.print("Enter file name: ");
              fileName = stdin.readLine();
              os.println(fileName);
              receiveFile(fileName);
              break;
          case 3:
              os.println("3");
              sync();
              break;
          }
      } catch (Exception e) {
          System.err.println("not valid input");
      }
  
      sock.close();
  }
  
  public static String selectAction() throws IOException {
      System.out.println("1. Send file.");
      System.out.println("2. Recieve file.");
      System.out.println("3. Sync files");
      System.out.print("\nMake selection: ");
  
      return stdin.readLine();
  }
  
  public static void sendFile(String fileName) {
      try {
  
          System.out.println(fileName);
          File myFile = new File(fileName);
          byte[] mybytearray = new byte[(int) myFile.length()];
  
          FileInputStream fis = new FileInputStream(myFile);
          BufferedInputStream bis = new BufferedInputStream(fis);
          // bis.read(mybytearray, 0, mybytearray.length);
  
          DataInputStream dis = new DataInputStream(bis);
          dis.readFully(mybytearray, 0, mybytearray.length);
  
          OutputStream os = sock.getOutputStream();
  
          // Sending file name and file size to the server
          DataOutputStream dos = new DataOutputStream(os);
          dos.writeUTF(myFile.getName());
          dos.writeLong(mybytearray.length);
          dos.write(mybytearray, 0, mybytearray.length);
          dos.flush();
          System.out.println("File " + fileName + " sent to Server.");
  
      } catch (Exception e) {
          System.err.println("File does not exist!   "+e);
      }
  }
  
  
  public static void sync() {
      try {
          String list = "";
          File folder = new File("C:/Users/shami_000/Desktop/ClientFiles");
          File[] listOfFiles = folder.listFiles();
          List&lt;String&gt; results = new ArrayList&lt;String&gt;();
  
          for (int i = 0; i &lt; listOfFiles.length; i++) {
              if (listOfFiles[i].isFile()) {
                  results.add(listOfFiles[i].getName());
              }
          }
          for (int i = 0; i &lt; results.size(); i++) {
              list = list + results.get(i) + ",";
          }
  
          OutputStream oss = sock.getOutputStream();
  
          // Sending file name and file size to the server
          DataOutputStream dos = new DataOutputStream(oss);
          dos.writeUTF(list);
          InputStream in = sock.getInputStream();
  
          DataInputStream clientData = new DataInputStream(in);
  
          String fileNames = clientData.readUTF();
          System.out.println(fileNames);
          List&lt;String&gt; fname = new ArrayList&lt;String&gt;();
          StringTokenizer st = new StringTokenizer(fileNames, ",");
          while (st.hasMoreTokens()) {
              String name = (String) st.nextElement();
              fname.add(name);
          }
          for(int i=0;i&lt;fname.size();i++){
              os.println("1");
              sendFile(path+fname.get(i));
          }
      } catch (Exception e) {
  
      }
  }
    }
  </code></pre>
  
  <p>The manual options DO work (like when I choose option 1 or 2, it does transfer files) but when I try to sync, the server successfully gets a list (a really long string with file names that is comma delimited), compares files with its own folder and returns a list of all the missing files back to the client.  The client does successfully receive the new list, BUT when I try to upload the files using the sendfile function, I get the error =/
  I had a feeling that there maybe some socket issues but I can't seem to pinpoint where that would be.</p>
  
  <p>This bit right here in FileClient:</p>
  
  <pre><code> for(int i=0;i&lt;fname.size();i++){
          os.println("1");
          sendFile(path+fname.get(i));
      }
  </code></pre>
  
  <p>Is the main issue, the server should printout "1" in the console, but it doesn't do that which means the server is not ready to receive the files (it never executes the receiveFile function).</p>