UDP server - global receive

Hi all,

A quick query - I can’t get an extremely simple UDP server to work on the ChipworkX Development system. I’m running the latest firmware 4.1.5.1 (Reflashed both tinybooter and tinyclr to ensure they are correct). The socket.Bind fails resulting in the Socket.Poll throwing an exception ErrorCode 10057. The thing is this is based on the example from Jens Kuhner’s book Expert .Net Micro Framework (2nd Edition).

Am I missing something with regards to the hardware set-up or am I misusing sockets in some way?

Thanks in advance for any help.
Michael.


using System;
using Microsoft.SPOT;
using System.Net;
using System.Net.Sockets;

using System.Text;

namespace ChipworkXEthernetUDP
{
    public class Program
    {
        private const int port = 2000;
        
        public static void Main()
        {
            using (Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            {
                EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, port);

                serverSocket.Bind(remoteEndPoint);

                if (serverSocket.Poll(-1, SelectMode.SelectRead))
                {
                    byte[] inBuffer = new byte[serverSocket.Available];
                    int count = serverSocket.ReceiveFrom(inBuffer, ref remoteEndPoint);
                    string message = new string(Encoding.UTF8.GetChars(inBuffer));
                    Debug.Print("Received '" + message + "'.");
                }
            }
        }
    }
}

Have you configured your ethernet port with MFDeploy?

Mike,

Yes - I’ve attached a screenshot from MFDeploy. As far as I am aware everything is correct. IP is static, DNS is not needed nor is the default gateway. (please correct me if I am wrong)

Edit - I should probably add that I have tried the project on a Fez Cobra board with the same result. This would indicate to me that the problem is software. Is there a chance that some of the library files on my machine are corrupt? (Given that UDP transmit seems to work fine).

Edit 2 - I just performed a complete reinstall of the GHI SDK, and a repair of Visual C# express. The results are the same - exception 10057. Also to note is the CLR_E_FAIL (8). The number following often varies. The next step is to try the dev kit with a completely different PC (fresh VS2010 install).

Are you able to ping the board from another host?

Yes - ping works across the network from multiple hosts.

Edit - tried a completely separate dev machine - recreated the project from scratch - exactly the same error.

If you have a sample project you could send across I will give it a try.

Can you try the same code using the emulator?

May seem silly, but try adding a gateway address.

Can you try to directly use .ReceiveFrom instead of .Poll? I did it that way in my DNS code.

See http://code.tinyclr.com/project/244/name-service---give-your-fez-a-real-network-name/ and search for SocketThread()

Wouter -
I am using ReceiveFrom -

int count = serverSocket.ReceiveFrom(inBuffer, ref remoteEndPoint);

Correct me if I’m wrong but the poll is merely to establish the presence of data? (I did remove the poll function and performed a direct read with no change).

Mike - I’ve tried adding a gateway address (pointed it at the router for what it’s worth). Still the same exception. Same with the remaining fields DNS etc…

Gus - Interesting outcome - It failed but raised a separate dialog, I’ve attached a screenshot for your reference. Hopefully this will help pinpoint the issue.

All the help is really appreciated. This seems like a startlingly simple thing compared to the GUI and touchscreen stuff I’ve been working with yet I can’t get it to work for me :slight_smile: I’m sure I’m missing something painfully obvious.

Edit - Could someone please try a quick compile of the original code above either on a Fez Cobra, ChipworkX Dev kit or in the emulator.

Would incorrect references cause issues but not get flagged up by the compiler?

Various examples of UDP handling include the following using statements:


using GHIElectronics.NETMF.Net.Sockets;
using GHIElectronics.NETMF.Net.NetworkInformation;
using Socket = GHIElectronics.NETMF.Net.Sockets.Socket;

The only reference I can find to add that matches these is the GHIElectronics.NETMF.W5100 which relates specifically to the WIZnet module.

All of the above is irrelevant when using the emulator of course.

So maybe “IPAddress.Any” is not supported. We will dig into this but can you try to receive from a specific IP please?

Gus - If I try specifying an IP in the EndPoint the following exception occurs on the socket.Bind function:

EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.13"), port);
                byte[] inBuffer = new byte[serverSocket.Available];
                int count;

                serverSocket.Bind(remoteEndPoint);

The thread ‘’ (0x2) has exited with code 0 (0x0).
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::bind [IP: 0000] ####
#### System.Net.Sockets.Socket::Bind [IP: 0016] ####
#### ChipworkXEthernetUDP.Program::Main [IP: 0026] ####
#### SocketException ErrorCode = 10049
#### SocketException ErrorCode = 10049
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10049
An unhandled exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll

Error Code 10049 means Address not available. Firewall blocking maybe?

I have verified the code, and you’re doing exactly the same as in my DNS code. When I run your code in emulator, I get no dialog… But I’m using DHCP and have a gateway in my network.

@ EriSan: could be, when I ran the code in the emulator I had to accept a windows dialog to allow network access to the emulator.

For those interested, here’s an enum with all the socket error codes. Might come in handy if you encounter socket errors.

Also located here: [url]http://code.tinyclr.com/project/346/socket-error-codes/[/url]


    public enum SocketErrorCodes
    {
      InterruptedFunctionCall = 10004,
      PermissionDenied = 10013,
      BadAddress = 10014,
      InvalidArgument = 10022,
      TooManyOpenFiles = 10024,
      ResourceTemporarilyUnavailable = 10035,
      OperationNowInProgress = 10036,
      OperationAlreadyInProgress = 10037,
      SocketOperationOnNonSocket = 10038,
      DestinationAddressRequired = 10039,
      MessgeTooLong = 10040,
      WrongProtocolType = 10041,
      BadProtocolOption = 10042,
      ProtocolNotSupported = 10043,
      SocketTypeNotSupported = 10044,
      OperationNotSupported = 10045,
      ProtocolFamilyNotSupported = 10046,
      AddressFamilyNotSupported = 10047,
      AddressInUse = 10048,
      AddressNotAvailable = 10049,
      NetworkIsDown = 10050,
      NetworkIsUnreachable = 10051,
      NetworkReset = 10052,
      ConnectionAborted = 10053,
      ConnectionResetByPeer = 10054,
      NoBufferSpaceAvailable = 10055,
      AlreadyConnected = 10056,
      NotConnected = 10057,
      CannotSendAfterShutdown = 10058,
      ConnectionTimedOut = 10060,
      ConnectionRefused = 10061,
      HostIsDown = 10064,
      HostUnreachable = 10065,
      TooManyProcesses = 10067,
      NetworkSubsystemIsUnavailable = 10091,
      UnsupportedVersion = 10092,
      NotInitialized = 10093,
      ShutdownInProgress = 10101,
      ClassTypeNotFound = 10109,
      HostNotFound = 11001,
      HostNotFoundTryAgain = 11002,
      NonRecoverableError = 11003,
      NoDataOfRequestedType = 11004
    }

1 Like

I’ve tried pretty much everything I can think of for the moment.

Wouter - when you ran the code in the emulator and you accepted the dialog for the windows firewall exception did you try placing a breakpoint at the socket.bind. Step through from there and monitor the socket object - the endpoint exception is raised for me at that point.

I would feel much much better knowing that it isn’t just me - on the other hand it would also be nice if it was just me and had a very simple solution… (to my embarrassment)

An update: Michael called the office and we wet through it all in details. It seems that the debugger is not able to read the “Socket” object for some strange reason. If you add “Socket” to the watch window or try to hover over it with the cursor then this put the debugger in an unresponsive state.

This will be looked at in future but UDP works fine, TX and RX.

Thanks to everyone who helped - the project can now continue… :slight_smile: