客户端与服务器通信问题c#
本文关键字:问题 通信 服务器 客户端 | 更新日期: 2023-09-27 18:16:41
我创建了一个相互连接的客户机和服务器。服务器能够将客户端发送回的消息发送给客户端(经过处理后)…然而……如果有两个客户端连接,它只将消息发送回最初发送消息的客户端(lol…)
我该如何解决这个问题,以便它从任何客户端发送消息到每个客户端?
我使用下面的示例作为起点,以获得客户机和服务器之间的连接:
客户端服务器通信
当我尝试以下操作时,我的程序卡住了:
服务器:
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch (Exception ex)
{
Debug.Print(ex.Message);
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
foreach (TcpClient c in ListOfClients)
{
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
clientStream.Write(message, 0, message.Length);
}
客户:private void boxChatArea_KeyPress(object sender, KeyPressEventArgs e)
{
char[] contentForServer = null;
boxChatArea.MaxLength = 4000;
if (e.KeyChar == (char)Keys.Enter)
{
client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), MainWindow.port);
client.Connect(serverEndPoint);
clientStream = client.GetStream();
contentForServer = boxChatArea.Text.ToCharArray();
byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(contentForServer);
clientStream.Write(bytesToSend, 0, bytesToSend.Length);
clientStream.Flush();
boxChatArea.Text = null;
StartListening();
}
}
public void StartListening()
{
HandleServerComm(client);
}
private void HandleServerComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
bytesRead = 0;
try
{
//FREEZES HERE - it doesn't freeze here without the loop that we added within the server...
bytesRead = clientStream.Read(message, 0, 4096);
}
catch (Exception ex)
{
Debug.Print(ex.Message);
//break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
}
if (bytesRead != 0)
{
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
string text = (encoder.GetString(message, 0, message.Length));
if (enterCount >= 1)
{
displayBoxChatArea.AppendText(Environment.NewLine);
displayBoxChatArea.AppendText(text);
//displayBoxChatArea.Text = text;
Application.DoEvents();
}
else
{
displayBoxChatArea.Text = text;
Application.DoEvents();
}
}
enterCount++;
tcpClient.Close();
}
您应该获取一个已连接的客户端列表。
试试这样做:
List<TcpClient> clients = new List<TcpClient>();
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
if(!clients.Contains(clients))
clients.Add(client);
}
}
编辑用户评论:
你的发送函数做错了:你必须先从一个客户端获取消息,然后再发送给每个客户端。
clientStream = tcpClient.GetStream(); // Not in foreach loop !!
// ...
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
foreach(TcpClient client in clients)
{
// Send message to client
st = client.GetStream();
st.Write(message, 0, message.Length);
}