title: Java socket server client to client file transfer
body: <p>I'm not sure if what I'm trying to do is possible.
  I want a socket server to run, multiple clients can connect to it. Client A drags and drops a file onto his GUI and client B gets a popup asking if he wants to receive the file. If agreed client A starts uploading the file and client B starts downloading the file directly. Is this possible? should the file first be transferred to the server? If I send a file to the server it works, but when I try to send a file to client B I get the following:</p>
  
  <pre><code>java.net.ConnectException: Connection timed out: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:579)
      at java.net.Socket.connect(Socket.java:528)
      at java.net.Socket.&lt;init&gt;(Socket.java:425)
      at java.net.Socket.&lt;init&gt;(Socket.java:241)
  </code></pre>
  
  <p>here is the upload class:</p>
  
  <pre><code>public Upload(String addr, int port, File filepath, OpenChatFrame frame) {
          super();
          try {
              file = filepath;
              ui = frame;
              socket = new Socket(InetAddress.getByName(addr), port);
              Out = socket.getOutputStream();
              In = new FileInputStream(filepath);
              length = file.length();
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  
      @Override
      public void run() {
          try {
              byte[] buffer = new byte[1024];
  
              int count;
  
              ui.jProgressBar1.setMaximum((int) length);
  
              while ((count = In.read(buffer)) &gt;= 0) {
                  Out.write(buffer, 0, count);
                  bytes += count;
                  ui.jProgressBar1.setValue(bytes);
  
              }
              Out.flush();
  
  
              if (In != null) {
                  In.close();
              }
              if (Out != null) {
                  Out.close();
              }
              if (socket != null) {
                  socket.close();
              }
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  </code></pre>
  
  <p>and here is the download class:</p>
  
  <pre><code>public Download(String saveTo, OpenChatFrame ui) {
      try {
          server = new ServerSocket(0);
          port = server.getLocalPort();
          this.saveTo = saveTo;
          this.ui = ui;
      } catch (IOException ex) {
          //TODO: catch
      }
  }
  
  @Override
  public void run() {
      try {
          socket = server.accept();
          System.out.println("Download : " + socket.getRemoteSocketAddress());
  
          In = socket.getInputStream();
          Out = new FileOutputStream(saveTo);
  
          byte[] buffer = new byte[1024];
          int count;
  
          while ((count = In.read(buffer)) &gt;= 0) {
              Out.write(buffer, 0, count);
          }
  
          Out.flush();
  
          File physicalFile = new File(saveTo);
          System.out.println("FILE FILENAME: " + physicalFile.getName());
          System.out.println("saveTo: " + saveTo);
  
  
          if (Out != null) {
              Out.close();
          }
          if (In != null) {
              In.close();
          }
          if (socket != null) {
              socket.close();
          }
      } catch (Exception ex) {
          //TODO: catch
      }
  }
  </code></pre>