是否可以使用tcp listener向所有客户端发送消息?
示例:
TCP侦听器正在运行,侦听器向客户端发送默认消息,client1 connect获取默认消息,client2 connect获取默认消息,如果client1或client2修改默认消息并将其发送给侦听器,则侦听器将修改后的消息发送到client1和client2。
答案 0 :(得分:1)
是否可以使用tcp listener向所有客户端发送消息?
答案是否定的。
您只使用套接字服务器等待新连接(侦听器)并接受它们。每次套接字服务器接受新连接时,它都会分配独立套接字客户端以参加远程端点。
while (keepRunning)
{
try
{
TcpClient socket = server.AcceptTcpClient(); //Socket Server accept a new connection and assigned an independent socket client.
if (keepRunning)
RequestManager.askForRequestAdnRunIt(socket, idLayout); //The Socket Client is attending at the remote endpoint
}
catch (Exception ex)
{
log.Error("Se detecto un error en el puerto para atencion de peticiones. ERR: " + ex.Message);
log.Error(ex.StackTrace);
}
}
因此,如果要向所有TCPClients套接字发送消息,则必须逐个发送消息,我建议通过异步方法发送消息以提高性能并避免瓶颈。检查我的answer以了解如何使用异步方法发送。