如何向多线程聊天服务器中的所有客户端发送消息?

时间:2014-10-05 01:08:09

标签: c# multithreading list sockets chat

很抱歉一遍又一遍地询问“相同”的事情。这是聊天的另一个版本。经过大量的搜索,我终于找到了如何将客户列表(或者至少我希望如此)传递给Chat函数。

但我不知道这是否应该起作用:

每次客户连接时:

clients.Add(clientSocket);

var ctThread = new System.Threading.Thread(() => Chat(clients));

Chat函数希望通过

正确接收客户端

public void Chat(List<TcpClient> clients)

然后将其写出来

 foreach (var client in clients)
    {
        writer.Write(message);
    } 

客户端有2个线程(不确定它们是否可以同时实际读/写)

    Thread ctThread = new Thread(Write);
    Thread ctThread2 = new Thread(Read);
    ctThread2.Start();
    ctThread.Start();

我是否正确地将客户列表传递给了该功能?它能真正正确地发送消息吗?因为现在服务器没有响应我在客户端上输入的任何内容。

完整代码:

服务器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.IO;

namespace MultiServeris
{
    class Multiserveris
    {
        static void Main(string[] args)
        {
            TcpListener ServerSocket = new TcpListener(1000);         
            ServerSocket.Start();                                     
            List<TcpClient> clients = new List<TcpClient>();
            Console.WriteLine("Server started.");
            while (true)
            {
                TcpClient clientSocket = ServerSocket.AcceptTcpClient();
                handleClient client = new handleClient();
                clients.Add(clientSocket);
                client.startClient(clientSocket,clients);
            }
        }
    }

    public class handleClient
    {
        TcpClient clientSocket;                                 
        public void startClient(TcpClient inClientSocket, List<TcpClient> clients)
        {
            this.clientSocket = inClientSocket;
            var ctThread = new System.Threading.Thread(() => Chat(clients)); 
        }

        public void Chat(List<TcpClient> clients)
        {
            BinaryReader reader = new BinaryReader(clientSocket.GetStream());
            BinaryWriter writer = new BinaryWriter(clientSocket.GetStream());
            while (true)
            {
                string message = reader.ReadString();
                foreach (var client in clients)
                {
                    writer.Write(message);
                } 
            }
        }
    }
}

客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading;


namespace Klientas
{
    class Klientas
    {
        public static void Write()
        {
            while (true)
            {
                TcpClient clientSocket = new TcpClient("localhost", 1000);
                string str = Console.ReadLine();
                BinaryWriter writer = new BinaryWriter(clientSocket.GetStream());
                writer.Write(str);
            }
        }
        public static void Read()
        {
            while (true)
            {
                TcpClient clientSocket = new TcpClient("localhost", 1000);
                BinaryReader reader = new BinaryReader(clientSocket.GetStream());
                string message = reader.ReadString();
                Console.WriteLine(message);
            }
        }

        static void Main(string[] args){
            Thread ctThread = new Thread(Write);
            Thread ctThread2 = new Thread(Read);
            ctThread2.Start();
            ctThread.Start();
           }
    }
}

1 个答案:

答案 0 :(得分:1)

TCP不是广播设计,这就是您必须遍历所有客户端的原因。更好的方法是使用支持brodcast的协议使用SignalR Framework,或者如果你想要裸机访问使用UDP。这是一个很棒的SignalR聊天示例。

https://dhavalupadhyaya.wordpress.com/tag/signalr-chat-example/