Unhandled exception in GHIElectronics.NETMF.W5100.dll

I have a simple application that tries to create a TCP server socket to listen for connections. The code is:


using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net;

using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.Sockets;
using GHIElectronics.NETMF.Net.NetworkInformation;

using Socket = GHIElectronics.NETMF.Net.Sockets.Socket;
 
namespace FEZ_Cobra_Console_Application1
{
    public class Program
    {
        public static void Main()
        {
            const Int32 c_port = 80;

            byte[] ip = { 192, 168, 1, 165 };
            byte[] subnet = { 255, 255, 255, 0 };
            byte[] gateway = { 192, 168, 1, 1 };
            byte[] mac = { 0x00, 0x21, 0x03, 0x80, 0x15, 0xE6 };
 
            NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
            NetworkInterface.EnableStaticDns(new byte[] { 192, 168, 0, 1 });

            Socket server = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, c_port);

            server.Bind(localEndPoint);
            server.Listen(1);
        }

    }
}

When I execute the code I get an exception on the following line:


            NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);

The exception is:

Can anyone explain why I am getting this exception?

Regards,
Synapsys

Why are you using W5100 DLL? That is only needed when using USBizi-devices that needs W5100 to run networking.

You have full blown socket support on your FEZ Cobra and you do not need that DLL.

Both, USBizi and EMX have the same interface so they both run the same socket code. It is just that larger devices run the full NET version (which is what you need) and smaller devices run the GHI version, w5100 (which you do not need)

Okay, I removed the reference to the W5100 DLL.

Since you indicated that the Cobra has fill blown socket support I attempted to setup an application similar to one I already use in regular .NET but there doesn’t seem to be assemblies for “System.Net” or “System.Net.Sockets”. I can’t find any reference to a socket anywhere in the NETMF.

All I am attempting to build here is a simple TCP server that listens for a client to connect and exchange data with.

Any suggestions?

Regards,
Synapsys

There are TCP and HTTP (server and client) examples that ship with NETMF SDK from Microsoft.

Look in your “my document” folder and you should see them

For example C:\Users\Gus Issa\Documents\Microsoft .NET Micro Framework 4.1\Samples

You can run the examples on the PC emulator first then then try them on cobra as well

…let me know if you got it to work

Hi Gus,

Thank you so much for pointing me to the Microsoft sample directory! It’s exactly what I have been looking for ;D

With knowledge of where the samples are I won’t be asking so many dumb questions :-[

I looked at the TCP server example and it did not take long for me to realize that I needed a reference to System in the project references!

I will do some testing tomorrow and let you know how it goes.

Regards,
Synapsys

Please note that the program will end. You should at least go into a loop and issue Accept methods against the socket.

The Listen method is not blocking. It just defines the length of the incoming call queue.

Hi Gus,

I got the TCP server to run properly and it now exchanges data with my PC using TCP. :smiley:

Do you know of any way to detect when a socket is no longer connected (i.e. the PC end disconnects)? The Microsoft help for the Socket class indicates there is a property named “Connected” but this property doesn’t appear to be available in NETMF.

Regards,
Synapsys

I do not know myself…maybe others can help…

Thanks Gus…

I found a way to detect disconnect although it’s a bit ugly. If I try to send data to a socket that has disconnected an exception is thrown. So my code can periodically try to send data and if the exception occurs I will know the connection was lost.

Does anyone know of a better way to handle this?

Regards,
Synapsys

Knowing when a remote socket has been terminated abnormally is one of the difficulties of TCP/IP.

If the remote socket is closed cleanly, by an actual close method, you will usually get a completed read with a length of zero.

But, if the close is not clean, such as when the power is pull on the remote, the fun starts.

To get to the answer quickly, send a heartbeat, as you have done, is the answer.

There an internal heartbeat available, on some operating systems, which will determine when the remote has been abnormally closed. But, the timing of this feature varies, and whether it is turned on or off by default varies.

Some versions of windows have it turned on by default, but the heartbeat period is two hours.

I doubt MF has this feature.

So… heartbeat be the answer.

I see you’re using port 80, so you probably want to create a HTTP server.

You can use the HttpListener class to handle requests. I’ve created a WebServer class that’s based on that HttpListener class. You can find the source at [url]http://blog.fastload-media.be/?p=45[/url]

Thanks for the replies Mike and Wouter Huysentruit :slight_smile:

The port 80 thing is old. I’m actually using port 9760 now.

I implemented the heartbeat and it works okay.

Regards,
Synapsys