我创建了一个相互连接的客户端和服务器。服务器能够发送客户端发送回客户端的消息(处理后)...但是......如果连接了两个客户端,它只会将消息发送回发送消息的客户端(哈哈......)
我如何解决这个问题,以便它从任何客户端向每个客户端发送消息?
我使用下面的示例作为获取客户端和服务器之间连接的起点:
当我尝试以下操作时,我的程序冻结了:
服务器:
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();
}
答案 0 :(得分:2)
您应该列出已连接的客户端列表 尝试这样的事情:
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);
}