Hello
I’ve wrote a simple code for TCP server on my raptor :
private bool InitEthernet()
{
if (!ethernet_ENC28.Interface.IsOpen)
{
ethernet_ENC28.Interface.Open();
}
NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_ENC28.Interface);
ethernet_ENC28.Interface.NetworkInterface.EnableStaticIP(DeviceIP, SubnetMask, DefultGateway);
ethernet_ENC28.DebugPrintEnabled = true;
string res = TCPHandler();
if (res == "True")
return true;
else
{
Error = res;
return false;
}
}
private string TCPHandler()
{
byte[] bytes = new Byte[1024];
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, SocketPort);
bool recieved= false;
string res = string.Empty;
listener.Bind(localEndPoint);
listener.Listen(1);
Socket handler = null;
try
{
while (!recieved)
{
handler = listener.Accept();
recieved = true;
}
byte[] clientData = new byte[1024 * 100];
int receivedBytesLen = handler.Receive(clientData);
return "True";
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
but I get this error :
Exception was thrown: System.Net.SocketException
on this line :
handler = listener.Accept();
Why !!!
can everybody help?
thanks
@ andre.m -
firmware : 4.2
and exception : System.Net.SocketException
_Peter
January 8, 2015, 3:56pm
3
@ Ehsan Ansari - Can you switch to DHCP and put a little wait loop and print out the IP settings, just to make sure you have IP working at all.
Something like this, the code does run on my raptor with an enc28 1.1 module:
// Fire up the events
NetworkChange.NetworkAvailabilityChanged += (a, b) => Debug.Print(DateTime.Now + " Network availability = " + b.IsAvailable.ToString());
NetworkChange.NetworkAddressChanged += (a, b) => Debug.Print(DateTime.Now + " Network address change ");
// Start the network
netif = new EthernetENC28J60(SPI.SPI_module.SPI2, (Cpu.Pin)37, (Cpu.Pin)32, (Cpu.Pin)7, 24000);
netif.Open();
netif.EnableDhcp();
netif.EnableDynamicDns();
// Waiting for an address
while (netif.IPAddress == "0.0.0.0")
{
Debug.Print(DateTime.Now + " Waiting for network address ...");
Thread.Sleep(250);
}
// Some network stats
Debug.Print(DateTime.Now + " New address for the Network Interface ");
Debug.Print(DateTime.Now + " Is DHCP enabled: " + netif.NetworkInterface.IsDhcpEnabled);
Debug.Print(DateTime.Now + " Is DynamicDnsEnabled enabled: " + netif.NetworkInterface.IsDynamicDnsEnabled);
Debug.Print(DateTime.Now + " NetworkInterfaceType " + netif.NetworkInterface.NetworkInterfaceType);
Debug.Print(DateTime.Now + " Network settings:");
Debug.Print(DateTime.Now + " IP Address: " + netif.NetworkInterface.IPAddress);
Debug.Print(DateTime.Now + " Subnet Mask: " + netif.NetworkInterface.SubnetMask);
Debug.Print(DateTime.Now + " Default Gateway: " + netif.NetworkInterface.GatewayAddress);
Debug.Print(DateTime.Now + " MAC Address: " + GetMACAddress(netif.NetworkInterface.PhysicalAddress));
Debug.Print(DateTime.Now + " Number of DNS servers: " + netif.NetworkInterface.DnsAddresses.Length);
for (int i = 0; i < netif.NetworkInterface.DnsAddresses.Length; i++)
Debug.Print(DateTime.Now + " DNS Server " + i.ToString() + ":" + netif.NetworkInterface.DnsAddresses[i]);
Debug.Print(DateTime.Now + " ------------------------------------------------------");
The settins are valid for connecting a ENC28 to socket 1 of the Raptor.
Edit: this is done in plain NetMf so without using the gadgeteer designer !!!
_Peter
January 8, 2015, 4:00pm
4
The output looks like this:
06/01/2011 00:10:50 Board: GHI Electronics FEZ Raptor 1.0
06/01/2011 00:10:50 OEM : Copyright (C) GHI Electronics, LLC
06/01/2011 00:10:50 SDK : 4.3.6.0
06/01/2011 00:10:50 Preparing the network…
06/01/2011 00:10:50 Waiting for network address …
06/01/2011 00:10:51 Waiting for network address …
06/01/2011 00:10:51 Waiting for network address …
06/01/2011 00:10:51 Network address change
06/01/2011 00:10:51 New address for the Network Interface
06/01/2011 00:10:51 Is DHCP enabled: True
06/01/2011 00:10:51 Is DynamicDnsEnabled enabled: True
06/01/2011 00:10:51 NetworkInterfaceType 6
06/01/2011 00:10:51 Network settings:
06/01/2011 00:10:51 IP Address: 192.168.39.13
06/01/2011 00:10:51 Subnet Mask: 255.255.255.0
06/01/2011 00:10:51 Default Gateway: 192.168.39.123
06/01/2011 00:10:51 MAC Address: 00-21-03-00-00-02
06/01/2011 00:10:51 Number of DNS servers: 1
06/01/2011 00:10:51 DNS Server 0:192.168.39.123
06/01/2011 00:10:51 ------------------------------------------------------
_Peter
January 8, 2015, 4:45pm
5
@ Ehsan Ansari - I extended your TcpHandler() routine to return the received client data as well.
And it did run just fine on my Raptor with enc28. The raptor is on firmware 4.3.6.0 and the code is run by VS2013.
Maybe you should consider getting the latest SDK. Here’s how .NET Micro Framework - GHI Electronics
private static string TCPHandler()
{
byte[] bytes = new Byte[1024];
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 80);
bool recieved = false;
string res = string.Empty;
listener.Bind(localEndPoint);
listener.Listen(1);
Socket handler = null;
try
{
while (!recieved)
{
handler = listener.Accept();
recieved = true;
}
byte[] clientData = new byte[1024 * 100];
int receivedBytesLen = handler.Receive(clientData);
if (receivedBytesLen == 0) return "Nothing!!";
res = "";
for (int i = 0; i < receivedBytesLen; i++)
{
res += (char)clientData[i];
}
return res;
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
With this output when I browse with my wifi connected android mobile phone:
The thread ‘’ (0x2) has exited with code 0 (0x0).
06/01/2011 00:54:31 Board: GHI Electronics FEZ Raptor 1.0
06/01/2011 00:54:31 OEM : Copyright (C) GHI Electronics, LLC
06/01/2011 00:54:31 SDK : 4.3.6.0
06/01/2011 00:54:31 Preparing the network…
06/01/2011 00:54:32 Waiting for network address …
06/01/2011 00:54:32 Waiting for network address …
06/01/2011 00:54:32 Waiting for network address …
06/01/2011 00:54:32 Waiting for network address …
06/01/2011 00:54:33 Waiting for network address …
06/01/2011 00:54:33 Waiting for network address …
06/01/2011 00:54:33 Waiting for network address …
06/01/2011 00:54:33 Waiting for network address …
06/01/2011 00:54:34 Network availability = True
06/01/2011 00:54:34 Waiting for network address …
06/01/2011 00:54:34 Waiting for network address …
06/01/2011 00:54:34 Waiting for network address …
06/01/2011 00:54:34 Waiting for network address …
06/01/2011 00:54:35 Waiting for network address …
06/01/2011 00:54:35 Waiting for network address …
06/01/2011 00:54:35 Waiting for network address …
06/01/2011 00:54:35 Waiting for network address …
06/01/2011 00:54:36 Waiting for network address …
06/01/2011 00:54:36 Waiting for network address …
06/01/2011 00:54:36 Waiting for network address …
06/01/2011 00:54:36 Waiting for network address …
06/01/2011 00:54:37 Waiting for network address …
06/01/2011 00:54:37 Waiting for network address …
06/01/2011 00:54:37 Waiting for network address …
06/01/2011 00:54:37 Waiting for network address …
06/01/2011 00:54:38 Network address change
06/01/2011 00:54:38 New address for the Network Interface
06/01/2011 00:54:38 Is DHCP enabled: True
06/01/2011 00:54:38 Is DynamicDnsEnabled enabled: True
06/01/2011 00:54:38 NetworkInterfaceType 6
06/01/2011 00:54:38 Network settings:
06/01/2011 00:54:38 IP Address: 192.168.39.13
06/01/2011 00:54:38 Subnet Mask: 255.255.255.0
06/01/2011 00:54:38 Default Gateway: 192.168.39.123
06/01/2011 00:54:38 MAC Address: 00-21-03-00-00-02
06/01/2011 00:54:38 Number of DNS servers: 1
06/01/2011 00:54:38 DNS Server 0:192.168.39.123
06/01/2011 00:54:38 ------------------------------------------------------
06/01/2011 00:54:38 Grabbing the time from … de.pool.ntp.org
01/08/2015 22:32:18 Date and time updated thru NTP
01/08/2015 22:32:35 Result of TcpHandler() … GET / HTTP/1.1
Host: 192.168.39.13
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/ ;q=0.8
User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; SM-G900F Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.93 Mobile Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4,de;q=0.2
01/08/2015 22:32:35 … the blue LED should be blinking now …
Hope this helps.
@ andre.m -
sorry for delay… I was in holiday
the error is : 10050 which is related to down network! but it cannot be correct because I ran previous version of my code so that the raptor was TCP client and my PC was TCP server on current network…
@ PiWi -
I switched to DHCP and it was amazing:
06/01/2011 00:20:02 Waiting for network address ...
06/01/2011 00:20:02 Waiting for network address ...
06/01/2011 00:20:03 Waiting for network address ...
06/01/2011 00:20:03 Waiting for network address ...
06/01/2011 00:20:03 Waiting for network address ...
06/01/2011 00:20:03 Waiting for network address ...
06/01/2011 00:20:04 Waiting for network address ...
06/01/2011 00:20:04 Waiting for network address ...
06/01/2011 00:20:04 Waiting for network address ...
06/01/2011 00:20:04 Waiting for network address ...
06/01/2011 00:20:05 Waiting for network address ...
06/01/2011 00:20:05 Waiting for network address ...
06/01/2011 00:20:05 Waiting for network address ...
DHCP cannot make valid IP…
it seems to be related to firmeware version. I will test it
_Peter
January 10, 2015, 5:29am
8
@ Ehsan Ansari - look here for more on using TCP Server or even a UDP Server …
I’ve found the network getting more stable with the latest FW updates. Watch out for the use of the gadgeteer webserver that still has a little flaw and won’t work, one still needs to manually fix, at least I had to do it still on 2.43.1.0 …
1 Like