[java] web proxy를 구현하려는데 응답이 안오네요;
글쓴이: rhaps0dy / 작성시간: 목, 2017/04/06 - 6:11오후
안녕하세요. 글 남기는건 처음인데 너무 답답해서 이렇게 글을 남겨봅니다. ㅠ
제가 만드려는 프록시는 아래와 같은 구조를 가지고 있구요.
client--------[proxy]---------server
클라이언트에서 local 프록시를 타고 서버로 요청이 되야하는데
포트포워딩까지는 되지만, 웹서버로 요청이나 응답이 오지 않네요.
참조한 코드는 아래와 같습니다.
import java.io.*; import java.net.*; /** * * @author jcgonzalez.com * */ public class ProxyMultiThread { public static void main(String[] args) { try { if (args.length != 3) throw new IllegalArgumentException("insuficient arguments"); // and the local port that we listen for connections on String host = args[0]; int remoteport = Integer.parseInt(args[1]); int localport = Integer.parseInt(args[2]); // Print a start-up message System.out.println("Starting proxy for " + host + ":" + remoteport + " on port " + localport); ServerSocket server = new ServerSocket(localport); while (true) { new ThreadProxy(server.accept(), host, remoteport); } } catch (Exception e) { System.err.println(e); System.err.println("Usage: java ProxyMultiThread " + "<host> <remoteport> <localport>"); } } } /** * Handles a socket connection to the proxy server from the client and uses 2 * threads to proxy between server and client * * @author jcgonzalez.com * */ class ThreadProxy extends Thread { private Socket sClient; private final String SERVER_URL; private final int SERVER_PORT; ThreadProxy(Socket sClient, String ServerUrl, int ServerPort) { this.SERVER_URL = ServerUrl; this.SERVER_PORT = ServerPort; this.sClient = sClient; this.start(); } @Override public void run() { try { final byte[] request = new byte[1024]; byte[] reply = new byte[4096]; final InputStream inFromClient = sClient.getInputStream(); final OutputStream outToClient = sClient.getOutputStream(); Socket client = null, server = null; // connects a socket to the server try { server = new Socket(SERVER_URL, SERVER_PORT); } catch (IOException e) { PrintWriter out = new PrintWriter(new OutputStreamWriter( outToClient)); out.flush(); throw new RuntimeException(e); } // a new thread to manage streams from server to client (DOWNLOAD) final InputStream inFromServer = server.getInputStream(); final OutputStream outToServer = server.getOutputStream(); // a new thread for uploading to the server new Thread() { public void run() { int bytes_read; try { while ((bytes_read = inFromClient.read(request)) != -1) { outToServer.write(request, 0, bytes_read); outToServer.flush(); //TODO CREATE YOUR LOGIC HERE } } catch (IOException e) { } try { outToServer.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); // current thread manages streams from server to client (DOWNLOAD) int bytes_read; try { while ((bytes_read = inFromServer.read(reply)) != -1) { outToClient.write(reply, 0, bytes_read); outToClient.flush(); //TODO CREATE YOUR LOGIC HERE } } catch (IOException e) { e.printStackTrace(); } finally { try { if (server != null) server.close(); if (client != null) client.close(); } catch (IOException e) { e.printStackTrace(); } } outToClient.close(); sClient.close(); } catch (IOException e) { e.printStackTrace(); } } }
출처:http://www.jcgonzalez.com/java-simple-proxy-socket-server-examples#4
아마도 바인딩해주는 쪽을 제가 잘 이해못해서 그러는듯 한데
어디가 잘못된걸까요? ㅠㅠ
Forums:
댓글 달기