I am having a similar issue to several older posts in connecting to the Ethernet and using a socket. My code is based on the Server Real Web Pages tutorial on the NETMF Projects Site at [url] microframeworkprojects.com. This site has code for the Cobra so I changed the pin definitions to the ChipworkX but changed nothing else. If I run this code with the DHCP code uncommented and the static code commented out, it obtains or renews the lease as appropriate. So I know that I am physically connected and talking at some level.
My problem is with the Sockets namespace and the socket class. The only library I have been able to find this namespace defined in is GHIElectronics.NETMF.W5100.
I have the following References
GHIElectronics.NETMF.Hardware.ChipworkX
GHIElectronics.NETMF.Net
GHIElectronics.NETMF.System
GHIElectronics.NETMF.W5100
Microsoft.SPOT.Hardware
Microsoft.SPOT.Native
Microsoft.SPOT.Net
mscorlib
When I run it, I get the following message:
An unhandled exception of type ‘System.NotSupportedException’ occurred in GHIElectronics.NETMF.W5100.dll
at the line where I try and create the socket:
Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
and the debug output gives me:
#### Exception System.NotSupportedException - CLR_E_NOT_SUPPORTED (1) ####
#### Message:
#### GHIElectronics.NETMF.Net.W5100::RegisterWrite [IP: 0015] ####
#### GHIElectronics.NETMF.Net.SocketNative::close [IP: 0007] ####
#### GHIElectronics.NETMF.Net.SocketNative::socket [IP: 008e] ####
#### GHIElectronics.NETMF.Net.Sockets.Socket::.ctor [IP: 002a] ####
#### ChipworkX_TCP_test.Program::Main [IP: 003c] ####
A first chance exception of type ‘System.NotSupportedException’ occurred in GHIElectronics.NETMF.W5100.dll
An unhandled exception of type ‘System.NotSupportedException’ occurred in GHIElectronics.NETMF.W5100.dll
I assume W5100 reference is wrong, but I can’t find another reference that gives me the Sockets class definition.
What dumb thing am I doing wrong?
Code follows.
using System;
using System.Threading;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.Sockets;
namespace ChipworkX_TCP_test
{
public class Program
{
static InputPort upButton = new InputPort((Cpu.Pin) ChipworkX.Pin.PA23, true, Port.ResistorMode.PullUp);
static InputPort selectButton = new InputPort((Cpu.Pin)ChipworkX.Pin.PA20, true, Port.ResistorMode.PullUp);
static InputPort downButton = new InputPort((Cpu.Pin)ChipworkX.Pin.PA18, true, Port.ResistorMode.PullUp);
public static void Main()
{
#region Check we have a valid NIC
// First, make sure we actually have a network interface to work with!
if (Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces().Length < 1)
{
Debug.Print("No Active network interfaces. Bombing out.");
Thread.CurrentThread.Abort();
}
#endregion
// OK, retrieve the network interface
Microsoft.SPOT.Net.NetworkInformation.NetworkInterface NI = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];
#region DHCP Code
// If DHCP is not enabled, then enable it and get an IP address, else renew the lease. Most of us have a DHCP server
// on a network, even at home (in the form of an internet modem or wifi router). If you want to use a static IP
// then comment out the following code in the "DHCP" region and uncomment the code in the "fixed IP" region.
//if (NI.IsDhcpEnabled == false)
//{
// Debug.Print("Enabling DHCP.");
// NI.EnableDhcp();
// Debug.Print("DCHP - IP Address = " + NI.IPAddress + " ... Net Mask = " + NI.SubnetMask + " ... Gateway = " + NI.GatewayAddress);
//}
//else
//{
// Debug.Print("Renewing DHCP lease.");
// NI.RenewDhcpLease();
// Debug.Print("DCHP - IP Address = " + NI.IPAddress + " ... Net Mask = " + NI.SubnetMask + " ... Gateway = " + NI.GatewayAddress);
//}
#endregion
#region Static IP code
// Uncomment the following line if you want to use a static IP address, and comment out the DHCP code region above
NI.EnableStaticIP("192.168.1.99", "255.255.255.0", "192.168.1.1");
#endregion
#region Create and Bind the listening socket
// Create the socket
Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Bind the listening socket to the port
IPAddress hostIP = IPAddress.Parse(NI.IPAddress);
IPEndPoint ep = new IPEndPoint(hostIP, 80);
listenSocket.Bind(ep);
#endregion
// Start listening
listenSocket.Listen(1);
// Main thread loop
while (true)
{
try
{
Debug.Print("listening...");
Socket newSock = listenSocket.Accept();
Debug.Print("Accepted a connection from " + newSock.RemoteEndPoint.ToString());
byte[] messageBytes = Encoding.UTF8.GetBytes(ButtonPage());
newSock.Send(messageBytes);
newSock.Close();
}
catch (Exception e)
{
Debug.Print(e.Message);
}
}
}
// Read the states of the Cobra buttons and build a web page showing their states
static string ButtonPage()
{
// Determine the states of the three cobra buttons
string ubs; if (upButton.Read() == false) ubs = "Pressed"; else ubs = "Released";
string sbs; if (selectButton.Read() == false) sbs = "Pressed"; else sbs = "Released";
string dbs; if (downButton.Read() == false) dbs = "Pressed"; else dbs = "Released";
// Build the web page
string s = "<html>\n"; // First the page type
s += "<head><title>Fez Cobra Test Page</title></head>\n"; // now the page header
s += "<body>\n"; // start the body
s += "<p>Up Button State = <i>" + ubs + "</i></p>"; // Up button, state in italics
s += "<p>Select Button State = <i>" + sbs + "</i></p>"; // Select button, state in italics
s += "<p>Down Button State = <i>" + dbs + "</i></p>"; // Down button, state in italics
s += "</body>"; // close the body section
s += "</html>"; // close the page type
return s;
}
}
}