Main Site Documentation

Strange Socket Behaviour


#1

Hi,

I’ve encountered a strange problem whilst using a socket class to receive data over a network.

I am using a Fez Cobra II with a built in Wifi RS9110 module and I’m using the following code to listen for incoming messages:


      Debug.Print("Listening on port " + _listeningPort.ToString());
      while (_isListening)
      {
        if (socket.Poll(-1, SelectMode.SelectRead))
        {
          Socket receive = socket.Accept();

          if (receive.Available > 0)
          {
            byte[] buffer = new byte[receive.Available];
            receive.Receive(buffer);
            ProcessStream(new String(Encoding.UTF8.GetChars(buffer)));
          }
        }
      }

Whilst testing the code I noticed that some messages that I broadcast from the server were being ignored. I tested the situation by adding a debug line that I could stock a break point on:


      Debug.Print("Listening on port " + _listeningPort.ToString());
      while (_isListening)
      {
        if (socket.Poll(-1, SelectMode.SelectRead))
        {
          Socket receive = socket.Accept();

          if (receive.Available > 0)
          {
            byte[] buffer = new byte[receive.Available];
            receive.Receive(buffer);
            ProcessStream(new String(Encoding.UTF8.GetChars(buffer)));
          }
          else
          {
            Debug.Print("Report Available=0");
          }
       }
      }

When is stopped on the breakpoint I looked at the value of receive.Available and it was not zero. I suspected that it might be a timing issue as sometimes it would work and sometimes it wouldn’t, so I decided to put a slight delay in just in case there was another thread at work:


      Debug.Print("Listening on port " + _listeningPort.ToString());
      while (_isListening)
      {
        if (socket.Poll(-1, SelectMode.SelectRead))
        {
          Thread.Sleep(100);
          Socket receive = socket.Accept();

          if (receive.Available > 0)
          {
            byte[] buffer = new byte[receive.Available];
            receive.Receive(buffer);
            ProcessStream(new String(Encoding.UTF8.GetChars(buffer)));
          }
          else
          {
            Debug.Print("Report Available=0");
          }
       }
      }

This seemed to solve the problem and data stopped getting lost. However, I’m not particularly happy with this solution as I don’t understand what is happening and I’m not convinced that it will work in all cases.

Can anyone shed any light on this?
Is there a more reliable way of doing it, or possible a better work-around?


#2

Hi @ andre.m

Thanks for your reply. I’m glad that I’m not the only one.

Are you suggesting in your answer that I should do away with the ‘Poll’ part and continually check to see if there is data waiting? Something like this:


      Debug.Print("Listening on port " + _listeningPort.ToString());
      while (_isListening)
      {
        if (socket.Available > 0)
        {
          byte[] buffer = new byte[socket.Available];
          socket.Receive(buffer);
          ProcessStream(new String(Encoding.UTF8.GetChars(buffer)));
        }
      }

Note: The ProcessStream method will piece together the information until it reaches an end-of-instruction character.