title: keep socket connection open after transfering a file to a client java
body: <p>In school we have a project where we have to send a file from server to a client. The problem we have is that when we transfer the file from the server to the client, the server shutsdown the connection. Here is our code so far:</p>
  
  <p>Client:</p>
  
  <pre><code>    public static void main(String argv[]) throws Exception {
  
      int port = 8888;  //default
      if (argv.length
              &gt; 0) {
          port = Integer.parseInt(argv[0]);
      }
  
      Socket clientSocket = new Socket("127.0.0.1", port);
      PrintStream outToServer = new PrintStream(
              clientSocket.getOutputStream());
      BufferedReader inFromServer = new BufferedReader(
              new InputStreamReader(clientSocket.getInputStream()));
  
  
  
      File f = new File("dictionaryPart.txt");
  
      String serverCommand = inFromServer.readLine().toLowerCase();
      while (serverCommand != null) {
          System.out.println(serverCommand);
          switch (serverCommand) {
              case "velkommen":
                  outToServer.println("Hej");
                  break;
              case "file":
                  f = copy(clientSocket, f);
                  String matches = CrackerCentralized.checkFile(f);
                  System.out.println(matches);
                  outToServer.println(matches);
                  break;
          }
          serverCommand = inFromServer.readLine().toLowerCase();
      }
  }
  
  public static File copy(Socket clientSocket, File f) {
      try {
          int filesize = 2022386;
          int bytesRead;
          int currentTot = 0;
  
          byte[] buffer = new byte[filesize];
          InputStream is = clientSocket.getInputStream();
          FileOutputStream fos = new FileOutputStream(f);
          BufferedOutputStream bos = new BufferedOutputStream(fos);
          bytesRead = is.read(buffer, 0, buffer.length);
          currentTot = bytesRead;
          while (bytesRead != -1) {
              bytesRead = is.read(buffer, currentTot, (buffer.length - currentTot));
              if (bytesRead &gt;= 0) {
                  currentTot += bytesRead;
              }
          }
          bos.write(buffer, 0, currentTot);
          bos.flush();
          bos.close();
      } catch (IOException ex) {
          Logger.getLogger(Master_Slave_Sockets_Client.class.getName()).log(Level.SEVERE, null, ex);
      }
      return f;
  }
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>    try {
          PrintStream outToClient = new PrintStream(connection.getOutputStream());
          OutputStream os = connection.getOutputStream();
          BufferedInputStream input = new BufferedInputStream(new FileInputStream(f));
          outToClient.println("file");
          final byte[] buffer = new byte[(int) f.length()];
          input.read(buffer, 0, buffer.length);
          os.write(buffer, 0, buffer.length);
          os.write(-1);
          os.flush();
          System.out.println(connection.isClosed());
          os.close();
          System.out.println(connection.isClosed());
      } catch (IOException ex) {
          Logger.getLogger(SocketController.class.getName()).log(Level.SEVERE, null, ex);
      }
  </code></pre>
  
  <p>I am aware of WHY the connection keeps on closing. We close the socket's output by writing </p>
  
  <pre><code>output.close();
  </code></pre>
  
  <p>But I don't know in what other way we must try to do this to make the server keep listening for the clients answer (match/no match), so that the server knows wether it should send more files or if the client was successful.. Is it even possible to send at file without shutting down the connection to the server? I've googled all day the last 2 days without any luck</p>
  
  <p>Thanks for reading and for your help. </p>