TCP connection on Gadgeteer to a node.js server

Oct 27, 2014 at 2:26 PM
Hi,
Please can you tel me why this code doesn't work ?

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;

namespace GadgeteerTCP
{
public partial class Program
{
    private const string dottedServerIPAddress = "127.0.0.1";
    private const int port = 8080;

    // This method is run when the mainboard is powered up or reset.   
    void ProgramStarted()
    {
        using (Socket clientSocket = new Socket(AddressFamily.InterNetwork,
                                                        SocketType.Stream,
                                                        ProtocolType.Tcp))
        {
            // Addressing
            IPAddress ipAddress = IPAddress.Parse(dottedServerIPAddress);
            IPEndPoint serverEndPoint = new IPEndPoint(ipAddress, port);
            // Connecting
            Debug.Print("Connecting to server " + serverEndPoint + ".");

            clientSocket.Connect(serverEndPoint);
            Debug.Print("Connected to server.");
            // Sending
            byte[] messageBytes = Encoding.UTF8.GetBytes("Hello World!");
            clientSocket.Send(messageBytes);
        }// the socket will be closed here
    }
}
}
same code works on Micro Framework and Console Application project but not on gadgeteer project.
Oct 27, 2014 at 3:59 PM
Hi,
you might want to share your hardware setup and also what does "does not work" mean so that we can better address the issue.

Jan
Oct 27, 2014 at 9:46 PM
Hi Jan,


I'm using Spider and netmf 2.4.
I get error when gadgeteer want to connect to the server. "clientSocket.Connect(serverEndPoint);", and I get this error:

Connecting to server 127.0.0.1:8080.
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message: 
#### Microsoft.SPOT.Net.SocketNative::poll [IP: 0000] ####
#### System.Net.Sockets.Socket::Poll [IP: 0011] ####
#### System.Net.Sockets.Socket::Connect [IP: 0029] ####
#### TCPexample1.Program::Main [IP: 002d] ####
#### SocketException ErrorCode = 10035
#### SocketException ErrorCode = 10035
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10035
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll