我正在学习c#socket编程.所以,我决定进行TCP聊天,基本的想法是A客户端将数据发送到服务器,然后服务器在线为所有客户端广播它(在这种情况下,所有客户端都在字典中).
当有1个客户端连接时,它按预期工作,当连接的客户端超过1时发生问题.
服务器:
class Program
{
static void Main(string[] args)
{
Dictionary<int,TcpClient> list_clients = new Dictionary<int,TcpClient> ();
int count = 1;
TcpListener ServerSocket = new TcpListener(IPAddress.Any, 5000);
ServerSocket.Start();
while (true)
{
TcpClient client = ServerSocket.AcceptTcpClient();
list_clients.Add(count, client);
Console.WriteLine("Someone connected!!");
count++;
Box box = new Box(client, list_clients);
Thread t = new Thread(handle_clients);
t.Start(box);
}
}
public static void handle_clients(object o)
{
Box box = (Box)o;
Dictionary<int, TcpClient> list_connections = box.list;
while (true)
{
NetworkStream stream = box.c.GetStream();
byte[] buffer = new …Run Code Online (Sandbox Code Playgroud)