Làm sao để kết nối đến http proxy trong mạng lan

em có lên mạng tìm được source code demo viết 1 http proxy đơn giản bằng socket

public class ProxyHttp {

    /**
     * @param args the command line arguments
     */
    private static final int PORT = 8080;
    // go~ localhost:8080
    public static void main(String[] args) {
        try {
            InetAddress baddress = InetAddress.getByName("127.0.0.1");
            ServerSocket server = new ServerSocket(PORT, 0, baddress);
            while (true) {
                System.out.println("MiniServer active " + PORT);
                new ThreadSocket(server.accept()).run();
            }
        } catch (Exception e) {
            e.getMessage();
        }
    }

}

class ThreadSocket extends Thread {

    private Socket insocket;

    ThreadSocket(Socket insocket) {
        this.insocket = insocket;
        this.start();
    }

    @Override
    public void run() {
        try {
            InputStream is = insocket.getInputStream();
            PrintWriter out = new PrintWriter(insocket.getOutputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String line;
            line = in.readLine();
            String request_method = line;
            System.out.println("HTTP-HEADER: " + line);
            line = "";
            // looks for post data
            int postDataI = -1;
            while ((line = in.readLine()) != null && (line.length() != 0)) {
                System.out.println("HTTP-HEADER: " + line);
                if (line.indexOf("Content-Length:") > -1) {
                    postDataI = new Integer(
                            line.substring(
                                    line.indexOf("Content-Length:") + 16,
                                    line.length())).intValue();
                }
            }
            String postData = "";
            // read the post data
            if (postDataI > 0) {
                char[] charArray = new char[postDataI];
                in.read(charArray, 0, postDataI);
                postData = new String(charArray);
            }
            out.println("HTTP/1.0 200 OK");
            out.println("Content-Type: text/html; charset=utf-8");
//            out.println("Location: https://daynhauhoc.com/");
//            out.println("Location: https://daynhauhoc.com/");
            out.println("Server: MINISERVER");
            // this blank line signals the end of the headers
            out.println("");
            // Send the HTML page
            out.println("<H1>Welcome to the Mini Server</H1>");
            out.println("<H2>Request Method->" + "" + "</H2>");
            out.println("<H2>Post->" + "" + "</H2>");
            out.println("<form name=\"input\" action=\"form_submited\" method=\"post\">");
            out.println("Username: <input type=\"text\" name=\"user\"><input type=\"submit\" value=\"Submit\"></form>");
            out.close();
            insocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

mọi thứ đều chạy ổn nếu chạy ở máy local , nhưng khi em sang 1 máy khác trong cùng mạng Lan gõ theo kiểu : IPmayserver:8080 thì ko kết nối đến được ạ ?
trong khi nếu chạy server tomcat thì có thể truy cập từ các máy trong mạng lan

bạn phải set server listen ở IP 0.0.0.0 hoặc IP LAN của máy bạn thì các máy khác trong LAN mới kết nối tới được. Ngoài ra xem lại cài đặt của firewall xem

3 Likes

chổ này

ServerSocket server = new ServerSocket(PORT, 0, baddress);

em thay bằng thế này thì lại listen được
vậy ý nghĩa của đoạn này có phải là tạo 1 serversocket sẽ lắng nghe mọi connect đến thông qua PORT đúng không ? vậy thêm 2 thông số backlog , address phía sau để làm gì ?
ServerSocket server = new ServerSocket(PORT);

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?