none
How to know when a Network Stream data was sent RRS feed

  • Question

  • How do I detect if my Network Stream has data?

    I have a thread that calls the function

    public void startClientListener()

    {

         while (true)

         {

               TcpListener currentLisner = _tcpListener.AcceptTcpClient();

               _connectedClients.add(currentLisner);

              ReadData(currentListener);

         }

    }

    private void ReadData(TcpListener currentListener)

    {

         byte[] bytes= new byte[256];

         string data = null;

         while((int i = currentListener.GetStream().Read(bytes, 0, bytes.Length))!=0)

         {  
              data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

              txtMessages.text += data + "\n";

         }

    }

    During the lifetime of the TcpClient in _connectedClients List, how can I detect that my client applications sent a data over to the, lets say for example _connectedClients[0].getStream()? is there any event in tcplistener or networkstream that I can subscribe so that my server can react if one of my client application sends data over the network stream.

    my code seems to not inform the server app to new data send after the

    ReadData(currentListener);

    is called, and there cannot perform appropriate action

    Saturday, April 13, 2013 3:21 PM

Answers

  • Hi, why not using a callback method, which will be triggered when data is received.

    http://stackoverflow.com/questions/14479052/asynchronous-tcpclient-c-sharp-messages-arent-sending

    Monday, April 15, 2013 7:40 AM