Problem with TCP client on a FEZ Spider kit

Hello, I am trying to implement a simple TCP client in .NETMF4.3 on my FEZ Spider kit.

I have the following code:

void ProgramStarted()
{
	ethernetJ11D.NetworkInterface.Open();
	ethernetJ11D.UseStaticIP("192.168.0.8", "255.255.255.0", "192.168.0.1");
	Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
	IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("192.168.0.5"), 7777);
	socket.Connect(endpoint);

	Debug.Print("connected");
	byte[] msg = new byte[] { 0,1,2,3,4 };
	socket.Send(msg);
}

The socket.Connect call throws a SocketException with error code 10050 (WSAENETDOWN: “Network is down”).
The ethernetJ11D.NetWorkInteface’s IsNetworkUp and IsNetworkConnected properties return true and I can ping the device without any problems. I’ve turned off the firewall, but that didn’t help.

Try this code:

using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

using GHINet = GHI.Premium.Net;

namespace TestApp
{
    public partial class Program
    {
        public GTM.GHIElectronics.Ethernet_ENC28 ethEnc = new GTM.GHIElectronics.Ethernet_ENC28(6);

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/
            if (!ethEnc.Interface.IsOpen)
                ethEnc.Interface.Open();

            Debug.Print("Subnet Mask: " + ethEnc.Interface.NetworkInterface.SubnetMask);

            if (!ethEnc.Interface.NetworkInterface.IsDhcpEnabled)
                ethEnc.Interface.NetworkInterface.EnableDhcp();

            Debug.Print("Subnet Mask: " + ethEnc.Interface.NetworkInterface.SubnetMask);

            Debug.Print("Assigning the network interface to the module");

            GHINet.NetworkInterfaceExtension.AssignNetworkingStackTo(ethEnc.Interface);

            ethEnc.Interface.CableConnectivityChanged += new GHINet.EthernetENC28J60.CableConnectivityChangedEventHandler(Interface_CableConnectivityChanged);
            ethEnc.Interface.NetworkAddressChanged += new GHINet.NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Interface_NetworkAddressChanged);
            

            
            
            
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
        }

        void Interface_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("Network Address Changed");
            Debug.Print("SubnetMask: " + ethEnc.Interface.NetworkInterface.SubnetMask);
            Debug.Print("IP Address: " + ethEnc.Interface.NetworkInterface.IPAddress);

        }

        void Interface_CableConnectivityChanged(object sender, GHINet.EthernetENC28J60.CableConnectivityEventArgs e)
        {
            Debug.Print("Cable Connectivity Changed");
            Debug.Print("The cable was " + (e.IsConnected ? "connected" : "removed"));            
        }
    }
}

1 Like

@ Mike - First of all, I don’t have any ENC28 module. Is it even compatible with Spider kits?

Secondly, I don’t use DHCP. So when I enable DHCP on my interface, the 0.0.0.0 value is assigned to the IP, subnet and gateway.

I’ve modified my program to be more verbose.

 
void ProgramStarted()
{
	Debug.Print("Program started");

	if (!ethernetJ11D.NetworkInterface.Opened)
	{
		Debug.Print("Opening interface");
		ethernetJ11D.NetworkInterface.Open();
		Debug.Print("Interface opened");
	}
	ethernetJ11D.UseStaticIP("192.168.0.8", "255.255.255.0", "192.168.0.1");
	Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
	IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("192.168.0.5"), 7777);
	ethernetJ11D.UseThisNetworkInterface();

	new Thread(() =>
	{
		try
		{
			//Thread.Sleep(3000);
			PrintNetworkState();
			Debug.Print("Connecting socket");
			socket.Connect(endpoint);
			Debug.Print("Socket connected");
			byte[] msg = new byte[] { 0, 1, 2, 3, 4 };
			socket.Send(msg);
		}
		catch (SocketException e) { Debug.Print("SocketException: " + e.ErrorCode); }
		catch (Exception e) { Debug.Print(e.GetType().ToString() + ": " + e.Message); }
		finally { PrintNetworkState(); }

	}).Start();
}

void PrintNetworkState()
{
	StringBuilder builder = new StringBuilder();
	builder.Append("Up=");
	builder.Append(ethernetJ11D.IsNetworkUp);
	builder.Append("; ");
	builder.Append("Connected=");
	builder.Append(ethernetJ11D.IsNetworkConnected);
	builder.Append("; ");
	builder.Append("IP=");
	builder.Append(ethernetJ11D.NetworkInterface.IPAddress);
	builder.Append("; ");
	builder.Append("Mask=");
	builder.Append(ethernetJ11D.NetworkInterface.SubnetMask);
	builder.Append("; ");
	builder.Append("GW=");
	builder.Append(ethernetJ11D.NetworkInterface.GatewayAddress);
	Debug.Print(builder.ToString());
}

For some reason now I get 10065 error code (WSAEHOSTUNREACH: No route to host).

[quote]Program started

Opening interface
Interface opened
Up=True; Connected=True; IP=192.168.0.8; Mask=255.255.255.0; GW=192.168.0.1
Connecting socket
SocketException: 10065
Up=True; Connected=True; IP=192.168.0.8; Mask=255.255.255.0; GW=192.168.0.1
[/quote]

Another curios observation: when I uncomment the Thread.Sleep invocation, the Socket.Connect succeeds, but the Socket.Send hangs for a few seconds and then throws the 10053 error code (WSAECONNABORTED: Software caused connection abort).

[quote]Program started

Opening interface
Interface opened
Up=True; Connected=True; IP=192.168.0.8; Mask=255.255.255.0; GW=192.168.0.1
Connecting socket
Socket connected
SocketException: 10053
Up=True; Connected=True; IP=192.168.0.8; Mask=255.255.255.0; GW=192.168.0.1[/quote]

In both of these cases I can still ping the Spider from my PC (192.168.0.5).

EDIT
Today I’ve forgotten to turn off my firewall. Now the Spider sends the data to my PC without any problems, but only with the Thread.Sleep call uncommented. Why is that?

after initialization, wait for until the interface shows that it’s IP address is no longer 0.0.0.0