简体   繁体   English

客户端可以同时向多台服务器发送消息吗?

[英]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.

相关问题 我不明白为什么我的一个客户端程序不能向服务器发送多于一条消息? - I don't understand why one of my client program can't send more than one message to the server? Netty可以为每个客户请求发送多个响应 - Can netty send more than one response per client request 服务器可以发送多个响应客户的请求吗? - Can server sends more than one responds to client's request? 我将如何更改此代码以允许在客户端和服务器之间发送多个用户输入消息 - How would i change this code to allow more than one user input message to be sent between the client and server Java TCP服务器无法从多个客户端接收消息 - Java TCP server cannot receive message from more than one client 编写命令以向 java 客户端服务器聊天应用程序中的一个或多个接收者发送消息 - write the command to send a message to one or more receivers in a java client server chat application Java:使用套接字从客户端向服务器发送多个图像文件 - Java: send more than one image file from client to server using socket 服务器如何使用Java将消息发送到客户端 - How server can send message to client in Java 服务器如何在不关闭的情况下接受来自客户端的多个请求? - How can a server take more than one request from a client without shutting down? 使用 Reactor Netty HttpClient,如何将客户端配置为使用 Flux 发布者将多个项目发送到服务器? - Using a Reactor Netty HttpClient, how can the client be configured to send more that one item using a Flux publisher to a server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM