[英]Can a client send a message to more than one server concurrently?
I know that we can use multithreading to ensure that a server can accept connections from many clients. 我知道我们可以使用多线程来确保服务器可以接受来自许多客户端的连接。 This code helps many clients connect to a server.
此代码可帮助许多客户端连接到服务器。
s = new ServerSocket(1500);
while(true)
{
Socket socket = s.accept();
new ClientThread1(socket).start();
}
But, can a client connect to more than one server the same way? 但是,客户端可以以相同的方式连接到多台服务器吗? Any help will be appreciated.
任何帮助将不胜感激。
You could fork a new Thread() for each server you want to connect to, and have each Thread connect to a different server. 您可以为要连接的每个服务器派生一个新的Thread(),并使每个线程连接到不同的服务器。
As far as each Thread is concerned, it will only be connecting to one server as normal. 就每个线程而言,它仅将正常连接到一台服务器。 You just need to use some global variables or something to make sure each Thread knows what server it is suppose to be connecting to.
您只需要使用一些全局变量或某些方法来确保每个线程都知道它应该连接到哪个服务器。
I can do so because connection is a tuple of client(ip:port) and server(ip:port). 我可以这样做是因为连接是客户端(ip:port)和服务器(ip:port)的元组。 If done concurrently and each tuple remains unique, it should be possible
如果同时完成并且每个元组保持唯一,则应该有可能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.