服务器端
static List<Client> clientList = new List<Client>();//储存了连接上的客户端
/// <summary>/// 广播消息
/// </summary>
/// <param name="message"></param>
public static void BroadcastMessage(string message)//广播方法,遍历List集合
{
var notConnectedList = new List<Client>();//存储动态连接的客户端
foreach (var client in clientList)
{
if (client.Connected)
{
client.SendMessage(message);//每个消息都广播出去
}
else
{
notConnectedList.Add(client);
}
}
foreach (var temp in notConnectedList)
{
clientList.Remove(temp);//移除所有的Client的
}
}
static void Main(string[] args)
{
Socket tcpSocket = new Socket(AddressFamily.InterNetwork, SocketT

本文介绍了使用C#进行TCP协议的Socket编程,包括服务器端的Client类设计,用于接收和发送消息,以及客户端如何连接服务器并发送、接收消息。通过创建Socket实例,设置连接和接收消息的线程,实现两端的数据通信。
最低0.47元/天 解锁文章
1649

被折叠的 条评论
为什么被折叠?



