我需要广播连接到服务器的客户端,使用TcpClient. 目前,我有一个 for 遍历整个客户列表,他们会一一收到消息。但是消息之间有一段时间,所以客户端不会同时收到消息。我认为使用多线程或协同程序有效,有人可以建议我吗?我附上广播的功能码
谢谢你们
public void broadCast(string sendMsg)
{
foreach (User user in clientList)
{
if (user.tcpClient != null)
{
m_NetStream = user.tcpClient.GetStream();
if (m_NetStream != null)
{
byte[] msgOut = Encoding.ASCII.GetBytes(sendMsg);
m_NetStream.Write(msgOut, 0, msgOut.Length);
}
else
{
ServerLog("Socket Error: Start at least one client first", Color.red);
return;
}
}
}
}