0

我正在网站上寻找我的问题,但似乎我还没有找到解决方案。我有运行良好的 Java 非阻塞客户端服务器程序。我从这里举了一个例子

我的问题是,如何使服务器也成为另一台服务器的客户端?现在,我的服务器将有 2 个监听端口来等待来自客户端的连接请求,这已经实现。现在,我想让服务器在同一时间可以使用不同的端口连接到另一台服务器。我不知道我怎么能做到这一点。希望有人能给出一些想法。非常感谢各位!

4

3 回答 3

1

从您的服务器代码向另一台服务器运行客户端。就如此容易。在这个场景中,你尝试过什么但没能完成的事情?您可能希望为此使用 Apache HTTP(它有同步和异步版本)客户端或Netty异步 HTTP 客户端。Apache http客户端教程在这里

于 2013-03-25T08:25:36.117 回答
0

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.

于 2013-04-10T13:07:09.060 回答
0

我认为代理应该解决您的问题。更多在这里

于 2013-03-25T08:33:23.143 回答