【问题标题】:C# Chat clients with multiple threads (read+write at the same time)C# 多线程聊天客户端(同时读+写)
【发布时间】:2014-11-29 09:06:02
【问题描述】:

如何让客户端从流中读取(对于发送到流中的其他客户端的消息),同时能够写入它们?

我尝试在客户端创建不同的线程(我做对了吗?),但是我仍然只能写入服务器,没有任何响应。这就是我现在得到的:

(服务器-客户端-客户端)

客户:

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());
                Console.WriteLine(reader.ReadString());
            }
        }

        static void Main(string[] args){
            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();                                    

            Console.WriteLine("Server started.");
            while (true)
            {
                TcpClient clientSocket = ServerSocket.AcceptTcpClient();       
                handleClient client = new handleClient();                      
                client.startClient(clientSocket);
            }


        }
    }

    public class handleClient
    {
        TcpClient clientSocket;                                   
        public void startClient(TcpClient inClientSocket)
        {
            this.clientSocket = inClientSocket;
            Thread ctThread = new Thread(Chat);                   
            ctThread.Start();
        }



        private void Chat()
        {                  
            while (true)
            {
                BinaryReader reader = new BinaryReader(clientSocket.GetStream());
                Console.WriteLine(reader.ReadString());
            }
        }



    }

}

【问题讨论】:

  • 您需要在服务器上维护客户端列表。
  • 使用SignalR,它会让你的生活无限轻松。
  • 虽然我为 Stackoerflow Answer 编写的示例代码是用 Java 来解决类似问题的,但可以将其改编为 C#。

标签: c# multithreading sockets asynchronous chat


【解决方案1】:

似乎您在服务器上没有任何代码可以向客户端发送消息。您需要维护一个已连接客户端的列表,并让服务器在收到消息时向符合条件的客户端发送消息。也不要使客户端成为控制台应用程序。与大多数聊天客户端项目不同,它实际上更难作为控制台应用程序。

要保留客户端列表,您可以像这样声明 TCP 客户端列表

static List<TcpClient> clients = new List<TcpClient>();

然后当客户端连接时,您将其添加到列表中

TcpClient clientSocket = ServerSocket.AcceptTcpClient();  
clients.Add(clientSocket);

然后,当您收到一条消息时,您会将其发送给所有客户端

BinaryReader reader = new BinaryReader(clientSocket.GetStream());
while(true)
{
    string message = reader.ReadString();
    foreach(var client in clients)
    {
        //send message to client
    }
}

现在请记住,在实践中您应该处理诸如断开连接以及从列表中添加和删除客户端应该是线程安全的(锁和所有)。

【讨论】:

  • 哦,所以客户自己不能只是继续阅读流?我应该如何创建客户列表?有什么基本的例子吗?我只需要服务器将它发送给每个人。
  • 客户端可以继续读取流。我的回答的哪一部分让你认为他们不能?
  • 好吧,既然你说我必须从服务器向客户端发送消息,我猜客户端不能像服务器一样继续读取相同的流并打印出来?这就是为什么我不知道如何做//send message to client 部分:D
  • 执行与从客户端向服务器发送消息相同的方式。这不像阅读流会卡住它或其他什么。
  • 我不确定您是否仍然可以看到我的 cmets 的通知,但是,您能告诉我代码的最后一部分(向所有客户端发送消息)应该在哪里吗?我将列表创建放在服务器的主目录中,并在同一函数中将列表添加到循环中。但是我不能在为每个用户创建的Chat() 中使用foreach(var client in clients),因为它没有定义。 (也不能使用static List...
【解决方案2】:

套接字客户端-服务器通信的良好起点:demo applicationlibrary。支持重新连接、客户端突然断开连接、消息广播等等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-17
    • 2013-04-09
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多