Main Site Documentation

Receive on UDP socket exception


#1

Hi Guys,

I have some simple test code here for a UDP Listener:


    public bool StartListener(int nPort)
    {
      m_ListenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

      try
      {
        // bind the listening socket to the port
        IPEndPoint ipLocalPort = new IPEndPoint(IPAddress.Any, nPort);
        m_ListenerSocket.Bind(ipLocalPort);
      }
      catch (Exception ex)
      {
        Debug.Print("Cannot connect to port");
        Debug.Print(ex.ToString());
        return false;
      }


      // start listening

      byte[] buffer = new byte[1024];

      while (true)
      {
        if (m_ListenerSocket.Poll(-1, SelectMode.SelectRead))
        {
          Debug.Print("Data Ready");

          m_ListenerSocket.Receive(buffer);

          Debug.Print(new string(System.Text.Encoding.UTF8.GetChars(buffer)));
        }
      }
      return true;
    }

This runs fine in the emulator no problems at all.

When run on the Cobra I get an exception on the .Receive() line:

Data Ready
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::recv [IP: 0000] ####
#### System.Net.Sockets.Socket::Receive [IP: 0018] ####
#### System.Net.Sockets.Socket::Receive [IP: 0010] ####
#### CADTestNetwork.CADUDPListener::StartListener [IP: 0065] ####
#### CADTestNetwork.Program::Main [IP: 000e] ####
#### SocketException ErrorCode = 10057
#### SocketException ErrorCode = 10057
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10057
An unhandled exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll

Does the Cobra not support the normal microsoft socket stuff and I need to use some FEZ alternative?

Thanks for any help

Andy


#2

Socket Error 10057 means: “Not Connected”.

Try setting your SocketType to Stream


#3

Hi,

Thanks for the reply.

You cannot useSocketType.Stream with ProtocolType.Udp.

Cheers

Andy


#4

Hi Guys,

Looks like you have to use RetrieveFrom() when running on the Cobra, no idea why this differs from the emulator! Anyone any ideas?

So the following works:


    public bool StartListener(int nPort)
    {
      m_ListenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
      EndPoint ipLocalPort;

      try
      {
        // bind the listening socket to the port
        ipLocalPort = new IPEndPoint(IPAddress.Any, nPort);
        m_ListenerSocket.Bind(ipLocalPort);
      }
      catch (Exception ex)
      {
        Debug.Print("Cannot connect to port");
        Debug.Print(ex.ToString());
        return false;
      }


      // start listening

      byte[] buffer = new byte[1024];

      while (true)
      {
        if (m_ListenerSocket.Poll(-1, SelectMode.SelectRead))
        {
          Debug.Print("Data Ready");

          m_ListenerSocket.ReceiveFrom(buffer, ref ipLocalPort);

          Debug.Print(new string(System.Text.Encoding.UTF8.GetChars(buffer)));
        }
      }
      return true;
    }
 

Cheers

Andy


#5

The emulator is “emulating” the sockets interface with a layer between, it may be doing something it shouldn’t. UDP is a connectionless protocol, you must use ReceiveFrom instead of Receive.

If I have an exception in a system function like Receive, I like to look at the docs to make sure I’m using it correctly :slight_smile:


#6

I must admit I jumped to the conclusion that receive should work as it worked in the emulator and also the endpoint was already bound to the socket!

I promise to read the documentation more closely next time :wink:


#7

I’m not giving you hell for it, just a friendly suggestion :slight_smile: The .NETMF docs are pretty sparse, so whatever info contained is usually pretty essential. Not lots of paragraphs/descriptions like the Win32 API for every single call :slight_smile:


#8

No Problem, you are quite correct.

UDP stuff I have done in .NET has always been done using UDPClient which doesn’t seem to be in NETMF (even though the documentation states it is!).

See I read the docs sometimes :slight_smile: