title: Using sockets in Java with a proxy
body: <p>I'm writing a very simple transport simulation (please don't ask why I use this approach, it's not really the point of my question).</p>
  
  <p>I have three threads running (although you could consider them as seperate programs). One as a client, one as a server, and one as a proxy.</p>
  
  <p>The first is used as the client, and the main code for that is given here:</p>
  
  <pre><code>try {
      Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(InetAddress.getLocalHost(), 4466));
      Socket socket = new Socket(proxy);
      InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getLocalHost(), 4456);
      socket.connect(socketAddress);
  
      // send data
      for (String straat : straten) {
          socket.getOutputStream().write(straat.getBytes());
      }
  
      socket.getOutputStream().flush();
      socket.getOutputStream().close();
      socket.close();
  
  } catch (Exception e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>The second is the server side, given here:</p>
  
  <pre><code>public void run() {
      try {
          ServerSocket ss = new ServerSocket(4456);
          Socket s = ss.accept();
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(s.getInputStream()));
  
          String incoming;
          while ((incoming = in.readLine()) != null) {
              panel.append(incoming + "\n");
          }
  
          panel.append("\n");
          s.getInputStream().close();
          s.close();
          ss.close();
  
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  }
  </code></pre>
  
  <p>And then there's the proxy-thread:</p>
  
  <pre><code>public void run() {
      try {
          ServerSocket ss = new ServerSocket(4466);
          Socket s = ss.accept();
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(s.getInputStream()));
  
          panel.append(verzenderId + "\n");
  
          String incoming;
          while ((incoming = in.readLine()) != null) {
              panel.append(incoming + "\n");
          }
  
          panel.append("\n");
          s.getInputStream().close();
          s.close();
          ss.close();
  
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Somehow, this won't work. The message is sent directly to the server, and the proxy doesn't receive any socket request.</p>
  
  <p>How can I make this work so that port 4466 becomes a proxy for the communication between the client-thread and the server-thread?</p>
  
  <p>The goal is to make this socket between the client and the server to become an SSLSocket, so that the proxy can't read anything that goes over it. Therefore, setting up two sockets, one between the client and the proxy and one between the proxy and the server, is not the solution I'm looking for.</p>
  
  <p>Thanks a lot in advance.</p>