C#多线程聊天客户端(同时读写)
如何使客户端在能够写入流的同时从流中读取(对于发送到流的其他客户端的消息) 我尝试在客户端创建不同的线程(我做得对吗?),但是我仍然只能向服务器写入,没有任何响应。这就是我现在得到的: (服务器客户端) 客户:C#多线程聊天客户端(同时读写),c#,multithreading,sockets,asynchronous,chat,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.T
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());
}
}
}
}
服务器上似乎没有任何代码可以向客户端发送消息。您需要维护已连接客户端的列表,并使服务器在收到消息时向符合条件的客户端发送消息。也不要将客户端设为控制台应用程序。与大多数聊天客户端项目不同,它实际上更难作为控制台应用程序来完成 要保留一个客户端列表,您需要声明一个TCP客户端列表,如下所示
static List<TcpClient> clients = new List<TcpClient>();
然后,当您收到一条消息时,您会将其发送给所有客户端
BinaryReader reader = new BinaryReader(clientSocket.GetStream());
while(true)
{
string message = reader.ReadString();
foreach(var client in clients)
{
//send message to client
}
}
现在请记住,在实践中,您应该处理断开连接之类的事情,并且从列表中添加和删除客户机应该是线程安全的(锁和全部)。套接字客户机-服务器通信的良好起点:和。支持重新连接、突然断开客户端连接捕获、消息广播等功能。您需要在服务器上维护客户端列表。使用
signar
,这将使您的生活变得更加轻松。虽然我为此编写的示例代码是用Java编写的,用于解决类似问题,但您可以将其应用于C,那么客户端本身就不能继续读取流了?我应该如何创建客户列表?有基本的例子吗?我只需要服务器把它发送给每个人。客户端可以继续读取流。我的回答的哪一部分让你认为他们不能?好吧,既然你说我必须从服务器向客户端发送消息,我想客户端不能像服务器一样,只是继续读取相同的流并打印出来?这就是为什么我不知道如何做//向客户端发送消息
部分:DDo,就像从客户端向服务器发送消息一样。我不确定你是否还能看到我评论的通知,但是,你能告诉我代码的最后一部分(向所有客户发送消息)应该在哪里吗?我将列表创建放在服务器的main中,并在相同的函数中将列表添加到循环中。但是,我不能在Chat()
中使用为每个用户创建的foreach(客户机中的var-client)
,因为它没有定义。(也不能使用静态列表…
)
BinaryReader reader = new BinaryReader(clientSocket.GetStream());
while(true)
{
string message = reader.ReadString();
foreach(var client in clients)
{
//send message to client
}
}