Using Sockets in Java with a Proxy Server

I am writing a very simple transport simulation (please do not ask why I use this approach, in fact this is not a question of my question).

I have three threads (although you can consider them as separate programs). One as a client, one as a server and one as a proxy.

The first is used as a client, and the main code is indicated here:

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(); } 

The second side of the server is here:

 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(); } } 

And then the proxy stream:

 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(); } } 

Somehow this will not work. The message is sent directly to the server, and the proxy server does not receive any socket request.

How can I do this job so that port 4466 becomes a proxy for communication between the client thread and the server thread?

The goal is to force this socket between the client and server to become an SSLSocket so that the proxy cannot read anything that sends it. Therefore, setting up two sockets, one between the client and the proxy server, and also between the proxy server and the server, is not the solution I'm looking for.

Thank you very much in advance.

+4
source share

All Articles