Mike

Mike

  • NA
  • 3
  • 0

TCP server client problem

Jan 22 2010 6:16 PM
Hi folks,

I have a server application that accepts connections from multiple clients by TCP IP (winforms). The server has a procedure that sends message to the clients (all the connected clients). Messages sent to any of the clients to the server are accepted and arrive, however when I try to send something back nothing happens.
This is the server code, it loops through an arraylist with TcpClient objects that are added when the clients connect (this seems to work right, when I debug I see this arraylist has Tcpclients):

public void SendMessageToAllClients(string msg)
{
   foreach (TcpClient Client in Clients)
      {
         NetworkStream clientStream = Client.GetStream();
         ASCIIEncoding encoder = new ASCIIEncoding();
         byte[] buffer = encoder.GetBytes(msg);

         clientStream.Write(buffer, 0, buffer.Length);
         clientStream.Flush();
      }
}


This is the receiving procedure on the client-side:

private void ReceiveMessages()
{
   // Receive the response from the server
   srReceiver = new StreamReader(tcpServer.GetStream());
   while (tcpServer.Connected)
      {
      this.Invoke(new ParseMsgCallback(this.ParseMsg), new object[] { srReceiver.ReadLine() });
      }
}

Waiting for messages happens in a seperate thread both client and serverside. The problem is however the client side just sits there doing nothing when the server sends something. When I debug the client I can see the connection is open, has the right IP address and port etc. (which makes sense because sending from client to server works)

Can anyone see what I'm missing?
 
Thanks in advance, Mike

Answers (2)