By Marco Minerva, translated by Mike Dodaro from the original version in Italian. .NET Gadgeteer offers to a very simple method to create a web server that will respond to GET requests, using the SetupWebEvent function supplied by Ethernet module as implemented by GHI Electronics. Examples are available on this blog, as in the post: .NET Gadgeteer Web Services; Picture, Audio, Application.
If we work with more complex scenarios, or need more versatility, we can create a multi threaded TCP server that will accept requests, process them, and respond to the client. These operations are similar to those of a classic TCP server realized with the full version of the .NET Framework .
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using Microsoft.SPOT; using Socket = System.Net.Sockets.Socket; namespace ServerExample { public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e); public class SocketServer { public const int DEFAULT_SERVER_PORT = 8080; private Socket socket; private int port; public event DataReceivedEventHandler DataReceived; public SocketServer() : this(DEFAULT_SERVER_PORT) { } public SocketServer(int port) { this.port = port; socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } public void Start() { IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port); socket.Bind(localEndPoint); socket.Listen(Int32.MaxValue); new Thread(StartServerInternal).Start(); } private void StartServerInternal() { while (true) { // Wait for a request from a client. Socket clientSocket = socket.Accept(); // Process the client request. var request = new ProcessClientRequest(this, clientSocket); request.Process(); } } private void OnDataReceived(DataReceivedEventArgs e) { if (DataReceived != null) DataReceived(this, e); } private class ProcessClientRequest { private Socket clientSocket; private SocketServer socket; public ProcessClientRequest(SocketServer socket, Socket clientSocket) { this.socket = socket; this.clientSocket = clientSocket; } public void Process() { // Handle the request in a new thread. new Thread(ProcessRequest).Start(); } private void ProcessRequest() { const int c_microsecondsPerSecond = 1000000; using (clientSocket) { while (true) { try { if (clientSocket.Poll(5 * c_microsecondsPerSecond, SelectMode.SelectRead)) { // If the buffer is zero-length, the connection has been closed // or terminated. if (clientSocket.Available == 0) break; byte[] buffer = new byte[clientSocket.Available]; int bytesRead = clientSocket.Receive(buffer, clientSocket.Available, SocketFlags.None); byte[] data = new byte[bytesRead]; buffer.CopyTo(data, 0); DataReceivedEventArgs args = new DataReceivedEventArgs( clientSocket.LocalEndPoint, clientSocket.RemoteEndPoint, data); socket.OnDataReceived(args); if (args.ResponseData != null) clientSocket.Send(args.ResponseData); if (args.Close) break; } } catch (Exception) { break; } } } } } } }
The ServerSocket class can specify the port to listen on. If not specified, the default is 8080. The ServerSocket.Start method creates a new thread that waits for logon from a client (the separate thread is necessary in order not to block the application). When a client connects to the server, the server creates an object that manages the request. When the socket. Accept method returns a value in a thread, the server resumes waiting for new logons.
The more important part of the job is carried out by the ProcessRequest method of the ProcessClientRequest class. Internally, it verifies the availability of data, and, in a positive case, processes the request on the socket, which gives back the byte array that has been transmitted from the client. After which, we create an object of type DataReceivedEventArgs (that as we will see) contains the data received and the IP addresses of sender and addressee. We then recall the ServerSocket.OnDataReceived method, by which the ServerSocket can be notified on arrival of new data.
If the class that has been registered for the DataReceived event (that we will illustrate in the following discussion), assigns a value to the ResponseData property of the DataReceivedEventArgs object, the byte array will be sent to the client. Analogously, if the Close property is set to true, the logon will be closed.
Here is the DataReceivedEventArgs class:
public class DataReceivedEventArgs : EventArgs { public EndPoint LocalEndPoint { get; private set; } public EndPoint RemoteEndPoint { get; private set; } public byte[] Data { get; private set; } public bool Close { get; set; } public byte[] ResponseData { get; set; } public DataReceivedEventArgs(EndPoint localEndPoint, EndPoint remoteEndPoint, byte[] data) { LocalEndPoint = localEndPoint; RemoteEndPoint = remoteEndPoint; if (data != null) { Data = new byte[data.Length]; data.CopyTo(Data, 0); } } }
Using the ServerSocket Class
Now we show a simple example using the ServerSocket class. After we connect the FEZ Spider and the Display T35 and Ethernet J11D modules, we initialize the network interface:
void ProgramStarted() { ethernet.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkUp); ethernet.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkDown); ethernet.UseDHCP(); Debug.Print("Program Started"); } private void ethernet_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state) { Debug.Print("Network Down!"); } private void ethernet_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state) { Debug.Print("Network Up!"); }
We also want to configure the display of two texts: one that shows the IP address assigned by DHCP and the other that shows the messages that are received from server TCP. Therefore we define a SetupWindow routine, which we will call inside ProgramStarted:
private Text txtAddress; private Text txtReceivedMessage; private void SetupWindow() { var window = display.WPFWindow; var baseFont = Resources.GetFont(Resources.FontResources.NinaB); Canvas canvas = new Canvas(); window.Child = canvas; txtAddress = new Text(baseFont, "Loading, please wait..."); canvas.Children.Add(txtAddress); Canvas.SetTop(txtAddress, 50); Canvas.SetLeft(txtAddress, 30); txtReceivedMessage = new Text(baseFont, string.Empty); txtReceivedMessage.Width = 300; txtReceivedMessage.TextWrap = true; canvas.Children.Add(txtReceivedMessage); Canvas.SetTop(txtReceivedMessage, 100); Canvas.SetLeft(txtReceivedMessage, 10); }
During loading of the application, we show a wait message, which will be replaced as soon as the network interface is ready:
private void ethernet_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state) { Debug.Print("Network Up!"); txtAddress.TextContent = "IP Address: " + ethernet.NetworkSettings.IPAddress + ", port 8080"; SocketServer server = new SocketServer(8080); server.DataReceived += new DataReceivedEventHandler(server_DataReceived); server.Start(); } private void server_DataReceived(object sender, DataReceivedEventArgs e) { string receivedMessage = BytesToString(e.Data); txtReceivedMessage.Dispatcher.BeginInvoke(delegate(object arg) { txtReceivedMessage.TextContent = "Received message: " + arg.ToString(); return null; }, receivedMessage); string response = "Response from server for the request '" + receivedMessage + "'"; e.ResponseData = System.Text.Encoding.UTF8.GetBytes(response); if (receivedMessage == "close") e.Close = true; } private string BytesToString(byte[] bytes) { string str = string.Empty; for (int i = 0; i < bytes.Length; ++i) str += (char)bytes[i]; return str; }
In this example, we create the server on PORT 8080, register the generated event when a message is received, and, finally, start the server. When data arrives, it invokes the server_DataReceived method . We use the BytesToString utility function to recover the string contained by the bytes received, and print the message to the display. We use notification by the Dispatcher.BeginInvoke method on the text case; this is necessary because the event is generated from one of the various threads of the interface, and therefore we must resort to this to update the thread that currently has the UI. Else an exception will result.
Next, we create a message and assign it to the ResponseData property of DataReceivedEventArgs: recall that in the server code, seen previously, this is the way to specify a response to send to the client. In this event we also close a logon in which the client has sent the “close” message.
A client for our Server
In order to test our server, we need a client application that can send a request. We therefore create a Console Application:
class Program { const string SERVER_IP = "192.168.1.102"; const int SERVER_PORT = 8080; static void Main(string[] args) { TcpClient client = new TcpClient(); Console.Write("Connecting... "); client.Connect(SERVER_IP, SERVER_PORT); Console.WriteLine("Connected\n"); using (Stream stream = client.GetStream()) { while (true) { Console.Write("Enter a string and press ENTER (empty string to exit): "); string message = Console.ReadLine(); if (string.IsNullOrEmpty(message)) break; byte[] data = Encoding.Default.GetBytes(message); Console.WriteLine("Sending... "); stream.Write(data, 0, data.Length); byte[] response = new byte[4096]; int bytesRead = stream.Read(response, 0, response.Length); Console.WriteLine("Response: " + Encoding.Default.GetString(response, 0, bytesRead)); Console.WriteLine(); } } client.Close(); } }
The only modification necessary to use this application is to change constant SERVER_IP based on IP address that is assigned to the .NET Gadgeteer application . A start time, we can send all the strings that we want to the server. The previous example will print to the display the received message and send an answer to the client.
The application is available for download.
#1 by aldi john on December 5, 2011 - 7:16 PM
Nice Works and great adventure !
FEZ Spider is using EMX that has Fully TCPIP
So It Should be able to Retrieve Email from Gmail using POP server using SSL
Nice if Your Next project idea is Gadgeteer send and retrieve email from Gmail or Yahoo, … Wow !
Cheers !
aldi john
#2 by Michael Dodaro on December 6, 2011 - 10:17 AM
I think we’ll give this a try. Thanks, Aldi John.
#3 by Marco Minerva on December 7, 2011 - 4:02 AM
Yes, thank you for the idea, we’ll work on it!
#4 by aldi john on December 8, 2011 - 7:14 AM
Thx Marco and Mike for being agree with the Idea ,
The challange on this project is : “the SSL”, but I believe FEZ Spider that based on EMX has Fully TCPIP capacity that make it available with SSL ? ( I heard SSL is impossible in netduino plus ? ), and I read from EMX manual , that EMX has SSL Support.
So Then We can Give a Control to : Lets say our robot ” via email ” , Cool !!
I can’t wait for trying also , my gadgeteer still on the way …!
#5 by Michael Dodaro on December 8, 2011 - 7:23 AM
You might also find this blog interesting: http://wp.me/p1XXrf-1V
He has a .NET Gadgeteer robot working now.
#6 by Sam on October 30, 2014 - 6:36 AM
Hi,I want to send a message from Gadgeteer as a client to a node.js server via TCP/IP connection. do you have any idea what is the wrong with my code?
using System;
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.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using GHI.Premium.Net;
namespace GadgeteerTCP
{
public partial class Program
{
private string dottedServerIPAddress ;
private const int port = 8080;
private Socket clientSocket;
private byte[] messageBytes;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
ethernet_J11D.DebugPrintEnabled = true;
ethernet_J11D.UseDHCP();
ethernet_J11D.UseThisNetworkInterface();
ethernet_J11D.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkUp);
}
void ethernet_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
{
initethernetConnection();
}
void initethernetConnection()
{
Debug.Print(“connecting to Ethernet “);
if (ethernet_J11D.Interface.IsOpen)
{
Debug.Print(“interface was open”);
}
else
{
Debug.Print(“interface was not open”);
ethernet_J11D.Interface.Open();
}
ethernet_J11D.Interface.CableConnectivityChanged += new EthernetBuiltIn.CableConnectivityChangedEventHandler((s, e) =>
{
Debug.Print(“Ethernet conn changed!”);
if (e.IsConnected) { Debug.Print(“ETHERNET connected!”); }
else { Debug.Print(“Ethernet disconnected..”); }
});
var settings = ethernet_J11D.NetworkSettings;
Debug.Print(“————————————————“);
//Debug.Print(“MAC: ” + ByteExten.ToHexString(settings.PhysicalAddress, “-“));
Debug.Print(“IP Address: ” + settings.IPAddress);
Debug.Print(“DHCP Enabled: ” + settings.IsDhcpEnabled);
Debug.Print(“Subnet Mask: ” + settings.SubnetMask);
Debug.Print(“Gateway: ” + settings.GatewayAddress);
Debug.Print(“————————————————“);
dottedServerIPAddress = settings.IPAddress;
using (clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp))
{
IPEndPoint RemoteHost = new IPEndPoint(Dns.GetHostEntry(dottedServerIPAddress).AddressList[0], port);
clientSocket.Connect(RemoteHost);
messageBytes = Encoding.UTF8.GetBytes(“Hello World!”);
clientSocket.Send(messageBytes);
byte[] inBuffer = new byte[100];
int count = clientSocket.Receive(inBuffer);
char[] chars = Encoding.UTF8.GetChars(inBuffer);
string str = new string(chars, 0, count);
Debug.Print(str);
}
}
}
}
I get this error :
#### Exception System.Net.Sockets.SocketException – CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::connect [IP: 0000] ####
#### System.Net.Sockets.Socket::Connect [IP: 001d] ####
#### GadgeteerTCP.Program::initethernetConnection [IP: 00e2] ####
#### GadgeteerTCP.Program::ethernet_NetworkUp [IP: 0005] ####
#### Gadgeteer.Modules.Module+NetworkModule::OnNetworkEvent [IP: 004d] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####
#### SocketException ErrorCode = 10054
#### SocketException ErrorCode = 10054
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10054
#### SocketException ErrorCode = 10054
Error invoking method “Gadgeteer.Modules.Module+NetworkModule” (check arguments to Program.BeginInvoke are correct)