在 Java 中使用带有代理的套接字

发布于 2024-11-07 21:46:30 字数 2112 浏览 0 评论 0原文

我正在编写一个非常简单的传输模拟(请不要问我为什么使用这种方法,这不是我问题的重点)。

我正在运行三个线程(尽管您可以将它们视为单独的程序)。一台作为客户端,一台作为服务器,一台作为代理。

第一个用作客户端,其主要代码在此处给出:

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

第二个是服务器端,在此处给出:

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

然后是代理线程:

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

不知何故,这不起作用。消息直接发送到服务器,代理不接收任何套接字请求。

我怎样才能做到这一点,以便端口 4466 成为客户端线程和服务器线程之间通信的代理?

目标是使客户端和服务器之间的此套接字成为 SSLSocket,以便代理无法读取通过它的任何内容。因此,设置两个套接字,一个在客户端和代理之间,一个在代理和服务器之间,并不是我正在寻找的解决方案。

预先非常感谢。

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).

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.

The first is used as the client, and the main code for that is given 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 is the server side, given 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 there's the proxy-thread:

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 won't work. The message is sent directly to the server, and the proxy doesn't receive any socket request.

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

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.

Thanks a lot in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文