title: send file using java NIO selector
body: <p>I am trying to send file from server using java NIO selector key, but from the client side I receive the whole file when I shutting down the server. </p>
  
  <p>Code for server side</p>
  
  <pre><code>    public static int DEFAULT_PORT = 1234;
  
  public static void main(String[] args) {
      String fileName = "10mb.txt";
      int port;
  
      try {
          port = Integer.parseInt(args[0]);
      } catch (Exception ex) {
          port = DEFAULT_PORT;
      }
      System.out.println("Listening for connections on port " + port);
  
      ServerSocketChannel serverChannel;
      Selector selector;
      try {
          serverChannel = ServerSocketChannel.open();
          ServerSocket ss = serverChannel.socket();
          InetSocketAddress address = new InetSocketAddress(port);
          ss.bind(address);
          serverChannel.configureBlocking(false);
          selector = Selector.open();
          serverChannel.register(selector, SelectionKey.OP_ACCEPT);
      } catch (IOException ex) {
          ex.printStackTrace();
          return;
      }
  
      while (true) {
  
          try {
              selector.select();
          } catch (IOException ex) {
              ex.printStackTrace();
              break;
          }
  
          Set readyKeys = selector.selectedKeys();
          Iterator iterator = readyKeys.iterator();
          while (iterator.hasNext()) {
  
              SelectionKey key = (SelectionKey) iterator.next();
  
              try {
  
                  if (key.isAcceptable()) {
                      ServerSocketChannel server = (ServerSocketChannel) key.channel();
                      SocketChannel client = server.accept();
                      System.out.println("Accepted connection from " + client);
                      client.configureBlocking(false);
                      SelectionKey key2 = client.register(selector, SelectionKey.OP_WRITE);
  
                      String filePath = (fileName);
                      File file = new File(filePath);
                      int fileSize = (int) file.length();
                      System.out.println("File size =" + fileSize);
                      FileInputStream fis;
                      ByteBuffer buffer = null;
  
                      try {
                          fis = new FileInputStream(file);
                          FileChannel fc = fis.getChannel();
                          buffer = ByteBuffer.allocate(fileSize);
                          try {
                              fc.read(buffer);
                          } catch (IOException ex) {
                              System.out.println("IO EX :" + ex);
                          }
                      } catch (FileNotFoundException ex) {
                          System.out.println("FileNotFoundEx :" + ex);
                      }
  
                      buffer.flip();
                      key2.attach(buffer);
  
                  } else if (key.isWritable()) {
  
                      SocketChannel client = (SocketChannel) key.channel();
                      ByteBuffer out = (ByteBuffer) key.attachment();
  
                      client.write(out);
                  }
  
              } catch (IOException ex) {
                  System.out.println("IO Exeption :" + ex);
                  key.cancel();
                  try {
                      key.channel().close();
                  } catch (IOException cex) {
                      System.out.println(cex);
                  }
              }
              iterator.remove();
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>Code for client side</p>
  
  <pre><code>public static void main(String[] args) throws IOException {
      String fileName = "10mb.txt";
      int bufferSize = 2048;
      int n = 0;
      int read = 0;
  
      SocketChannel socket = SocketChannel.open(new InetSocketAddress(1234));
      String filePath = (fileName);
  
      FileOutputStream out = new FileOutputStream(filePath);
      FileChannel file = out.getChannel();
      ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
  
      long startTime = System.currentTimeMillis();
  
  
      while ((read = socket.read(buffer)) &gt; 0) {
          buffer.flip();
  
          file.write(buffer);
  
          n = n + read;
  
          buffer.clear();
  
          System.out.println(n);
      }
      long endTime = System.currentTimeMillis();
      long difference = endTime - startTime;
      System.out.println("File with " + n + " bytes downloaded in " + difference + " ms");
      socket.close();
  
      file.close();
  
      out.close();
  }
  </code></pre>
  
  <p>I appreciates your help.</p>