简体   繁体   English

如何使用代理连接服务器?

[英]How to connect server using proxy?

I made a server-client chat program in JAVA. 我用JAVA编写了一个服务器-客户端聊天程序。 Both client and server are running on the same machine. 客户端和服务器都在同一台计算机上运行。 Now I want to use proxy server between them. 现在,我想在它们之间使用代理服务器。 eg; 例如; client connects to server via proxy, not by client's actual IP Address. 客户端通过代理连接到服务器,而不是通过客户端的实际IP地址。 I don't know if there is any library available to do this task or not. 我不知道是否有任何可用的库来执行此任务。 And I don't know which protocol should I use!! 而且我不知道我应该使用哪种协议!

//Client Class
Socket clientSocket = new Socket(InetAddress.getByName(ServerIP, 6789));

//Server Class
ServerSocket serverSocket = new ServerSocket(6789);
Socket socket = serverSocket.accept();

/********CONNECTION ESTABLISHED*******************************/

Now here I want to send connection request to server as a proxy instead of client IP Address on the same machine. 现在,在这里我想将连接请求作为代理而不是同一台计算机上的客户端IP地址发送到服务器。

TCP sockets are low-level enough to not have any proxy mechanism on their own level. TCP套接字的级别很低,以至于在自己的级别上没有任何代理机制。 Higher-level protocols (HTTP, FTP etc) can be proxied easily, but TCP not. 可以轻松代理更高级别的协议(HTTP,FTP等),而不能代理TCP。

However, you may look at SOCKS5 implementations. 但是,您可以查看SOCKS5实现。 There is at least one implementation that can wrap unmodified TCP client application and route it via SOCKS5 proxy to unmodified TCP server application. 至少有一种实现可以包装未修改的TCP客户端应用程序,并将其通过SOCKS5代理路由到未修改的TCP服务器应用程序。 I doubt this is the solution you're looking at, though. 我怀疑这是您正在寻找的解决方案。

TLDR: TCP connections can't be proxied using out of the box JDK APIs. TLDR:无法使用现成的JDK API代理TCP连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM