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 = â127.0.0.1â;
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);
}
}
}
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)
@ sam_simsim - The 10054 error code usually indicates that the remote host closed the connection. Are you sure that the server is functioning properly?
I have an node.js server. I have tesed the server and it works with a node.js and netmf client but only on gadgeteer i get this error :(.
the code for the server:
var net = require(ânetâ);
var PORT = 8080;
net.createServer(function(sock) {
console.log('CONNECTED: ' + ':'+ sock.remotePort);
sock.on('data', function(data) {
console.log('DATA ' + ': ' + data);
// Write the data back to the socket, the client will receive it as data from the server
sock.write('You said "' + data + '"');
});
}).listen(PORT);
console.log('Server listening on â + â:â+ PORT);
@ sam_simsim - Can you try to connect to other servers and see if the issue remains? Can you use wireshark to see if what the device is sending and receiving?
Just want to point out that we have code formatting in the forum posts. This will make you post so much better. It is a button with â101010â icon. You can always go back to your published message and edit it.
I tried to connect my gadgeteer to a netmf server on C#. I get the same error. I tried to connect the server with netmf client and it worked perfectly. :wall:
Sam, just for the record: you are aware of the fact that 127.0.0.1 is a local loopback address and it is used to test communication on same local device?
You must replace the 127.0.0.1 ip address with the ip address of your device which is listening for incoming sockets. Start debugging from there
Iâm just confused because first you say if i use dottedServerIPAddress = settings.IPAddress; so itâs mean that i want to connect to my own device then you say i need to have the IP-adress for the device ? the method settings.IPAdress generate the IP-addess for the device and itâs generate 192.168.1.5 for this time.
A first chance exception of type âSystem.Net.Sockets.SocketExceptionâ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10054
An unhandled exception of type âSystem.Net.Sockets.SocketExceptionâ occurred in Microsoft.SPOT.Net.dll
Sam, sorry for the confusion it is not my intention!
Let me try to get things clear
Your remote device is running the socket server (receive application) to which you want to connect from your netmf device, right? Suppose your remote device is having ip address 192.168.1.100 and your netmf is having 192.168.1.5 as ip address.
So in your software you must use 192.168.1.100 (the address of your node.js server) as your ip endpoint.
Edit:
The error youâre getting is because your netmf tries to connect to a socket on your own netmf device which isnât listening.
Patrick I have two services now but both works on localhost :/. I donât know how to make them to work for my gadgeteer. They just listning on port and any IP- address who want to connect with the server. I tried to listen on 192.168.1.5 wich is the gadgeteer IP-address, but the node.js server does not want to work and gave me an error. but it works fine if itâs listning on 127.0.0.1(localhost). the other one is a netmf server and it listen on any socket too listeningSocket.Bind(new IPEndPoint(IPAddress.Any, port)), and when I try to write listeningSocket.Bind(new IPEndPoint(Dns.GetHostEntry(dottedServerIPAddress).AddressList[0], port)) it gives my an error :/.
node.js server:
var net = require(ânetâ);
var HOST = â127.0.0.1â;
var PORT = 8080;
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
// Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
client.write('I am Chuck Norris!');
});
// Add a âdataâ event handler for the client socket
// data is what the server sent to this socket
client.on(âdataâ, function(data) {
console.log('DATA: ' + data);
// Close the client socket completely
client.destroy();
});
// Add a âcloseâ event handler for the client socket
client.on(âcloseâ, function() {
console.log(âConnection closedâ);
});
netmf server:
public class Program
{
private const int port = 8080;
public static void Main()
{
using (Socket listeningSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp))
{
listeningSocket.Bind(new IPEndPoint(IPAddress.Any, port));
Debug.Print("Listening for a client...");
listeningSocket.Listen(1);
using (Socket communicationSocket = listeningSocket.Accept())
{
Debug.Print("Connected to client.");
//wait infinitely to get a response
communicationSocket.ReceiveTimeout = -1;
byte[] inBuffer = new byte[1000];
int count = communicationSocket.Receive(inBuffer);
string message = new string(Encoding.UTF8.GetChars(inBuffer));
Debug.Print("Received '" + message + "'.");
}
}
}
}
}