我正在网站上寻找我的问题,但似乎我还没有找到解决方案。我有运行良好的 Java 非阻塞客户端服务器程序。我从这里举了一个例子
我的问题是,如何使服务器也成为另一台服务器的客户端?现在,我的服务器将有 2 个监听端口来等待来自客户端的连接请求,这已经实现。现在,我想让服务器在同一时间可以使用不同的端口连接到另一台服务器。我不知道我怎么能做到这一点。希望有人能给出一些想法。非常感谢各位!
我正在网站上寻找我的问题,但似乎我还没有找到解决方案。我有运行良好的 Java 非阻塞客户端服务器程序。我从这里举了一个例子
我的问题是,如何使服务器也成为另一台服务器的客户端?现在,我的服务器将有 2 个监听端口来等待来自客户端的连接请求,这已经实现。现在,我想让服务器在同一时间可以使用不同的端口连接到另一台服务器。我不知道我怎么能做到这一点。希望有人能给出一些想法。非常感谢各位!
Yes, you can put client code into a server. Just like you could download an HTML page from within a Java Servlet.
Your server should have a "handler" component that processes incoming requests from your client. Inside this handler code, you can put your client code which connects to a different server.
I guess what confuses you is, that the incoming connection and the outgoing connection are now both handled in the same poll-the-selector loop, or? You can attach objects to the selector key, as far as I remember. Use that to signal that this is not an incoming connection, but an outgoing connection, and handle all outgoing connections differently from incoming connections. You may even have to connect these incoming and outgoing connections somehow, so you know which outgoing connection matches which incoming connections.
我认为代理应该解决您的问题。更多在这里