Gadgeteer tcp_client

Oct 31, 2014 at 4:35 PM
I need help. I can't connect the gadgeteer to a tcp-server!!

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 System.Net;
using System.Net.Sockets;
using Gadgeteer.Modules.GHIElectronics;
using GHI.Premium.Net;

namespace GadgeteerTCP_A
{
public partial class Program
{
    private Socket socket;
    private Thread ReceiveThread;
    private byte[] receiveBuffer = new byte[4096];
    private string host = "192.168.1.99";
    private int port = 8080;
    // This method is run when the mainboard is powered up or reset.   
    void ProgramStarted()
    {


        initethernetConnection();

        Connect(host, port);

    }



    void Connect(string server, int port)
    {

        try
        {

            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(new IPEndPoint(IPAddress.Parse(server), port));

            ReceiveThread = new Thread(Receive);

            ReceiveThread.Start();

            Send("Hello World!");



        }

        catch (Exception x)
        {

        }





    }



    void Send(string message)
    {

        socket.Send(new System.Text.UTF8Encoding().GetBytes(message));

    }



    public void Receive()
    {

        socket.Receive(receiveBuffer);
        char[] chars = new System.Text.UTF8Encoding().GetChars(receiveBuffer);

        //depends on how you want to process the chars, here we just concatenate a string with them.
        string data = "";

        for (int i = 0; i < chars.Length; i++)
        {

            data += chars[i];

        }
        Debug.Print(data);

    }
      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.."); }
            });

        ethernet_J11D.DebugPrintEnabled = true;
        ethernet_J11D.UseDHCP();
        ethernet_J11D.UseThisNetworkInterface();
        Thread.Sleep(3000);
        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("------------------------------------------------");


        host = settings.IPAddress;

        }
}
}

I get this error:

connecting to Ethernet

interface was open

IP Address: 192.168.1.5
DHCP Enabled: True
Subnet Mask: 255.255.255.0

Gateway: 192.168.1.254

#### 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_A.Program::Connect [IP: 001b] ####
#### GadgeteerTCP_A.Program::ProgramStarted [IP: 0012] ####
#### 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
The thread '<No Name>' (0x3) has exited with code 0 (0x0).
The program '[35] Micro Framework application: Managed' has exited with code 0 (0x0).