具有多个线程的C#聊天客户端(同时读取+写入)

时间:2014-10-04 14:12:00

标签: c# multithreading sockets asynchronous chat

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

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

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

客户端:

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());
            }
        }



    }

}

2 个答案:

答案 0 :(得分:2)

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

要保留客户列表,请声明此类

的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
    }
}

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

答案 1 :(得分:0)

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