Next step to create a WiFi socket between PC and FEZ II Cobra?

We are trying to create a peer-to-peer connection between a FEZ Cobra II (WiFi) and a desktop computer. We have completed the following steps:

[ul]Wait for the wireless connectivity to change.[/ul]

The problem is that we are stuck after creating the socket. We don’t know how then to connect to the socket from the desktop computer.

Once we’ve created the socket (by running the code shown below), we try to connect as follows. We open PowerShell on the desktop computer and run:

[ul]netsh interface ip delete arpcache[/ul]
[ul]netsh wlan show networks mode=bssid[/ul]

This output shows that the SSID that we created is visible. This is good. Our ad-hoc network is broadcasting.Then in Windows 8 we do the following:

[ul]open Network and Sharing Center[/ul]
[ul]Set up a new connection or network[/ul]
[ul]Manually connect to a wireless network[/ul]
[ul]Network name: YOURSSID[/ul]
[ul]Security type: No authentication (Open)[/ul]
[ul]Leave other defaults[/ul]

And the we run the following in PowerShell:

[ul]netsh wlan connect ssid=YOURSSID name=PROFILENAME[/ul]
[ul]e.g. netsh wlan connect ssid=MyAdHocHost129 name=MyAdHocHost129[/ul]

The results of the PowerShell command say:

This is good too! The Desktop Computer is connected to the Cobra WiFi. Hooray! Now, the problem is, now that we made a successful connection request with the Cobra II WiFi, what do we do next to create a socket? When we try to create a socket, the listener just sits waiting for a client request, and we haven’t been able to figure out how to contact the listener. Here is our full code.

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using GHI.Hardware.G120;
using GHI.Premium.Net;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace MFConsoleApplication3
    public class Program
        private static WiFiRS9110 _wifi;
        private static readonly ManualResetEvent mre = new ManualResetEvent(false);

        private const string AdHocHostName = "MyAdHocHost";
        private const string IpAddress = "";
        private const string SubnetMask = "";
        private const string GatewayAddress = "";

        public static void Main()

                _wifi = CreateWifiDriver();
                ConfigureIpAddress(_wifi, false);

                new Thread(CreateSocket).Start();
                SetupAdHocHost(_wifi, AdHocHostName);


                /* --------------------------------------
                 * Open PowerShell and run:
                 netsh interface ip delete arpcache
                 netsh wlan show networks mode=bssid
                 * Then open Network and Sharing Center
                 * Set up a new connection or network
                 * Manually connect to a wireless network
                 * Network name: YOURSSID
                 * Security type: No authentication (Open)
                 * Leave other defaults
                 * Next
                 * Open PowerShell and run:
                 netsh wlan connect ssid=YOURSSID name=PROFILENAME
                 e.g. netsh wlan connect ssid=MyAdHocHost129 name=MyAdHocHost129
                 * "Connection request was completed successfully."
                 * This means that the Desktop Computer is connected to the Cobra WiFi.
                 * However, we haven't been able to send data via TCP.
                 * I.e. the CreateSocket() method never goes past Waiting for client...
                 -------------------------------------- */
            catch (Exception e)

        private static void CreateSocket()

            if (_wifi.IsLinkConnected)
                var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress hostIp = IPAddress.Parse(_wifi.NetworkInterface.IPAddress);
                var hostEndPoint = new IPEndPoint(hostIp, 80);

                // does this need to wait too?
                Debug.Print("Waiting for client...");

                Socket client = socket.Accept();
                var clientEndPoint = client.RemoteEndPoint as IPEndPoint;
                if (clientEndPoint != null)
                    Debug.Print("Connected with " + clientEndPoint.Address + " at port " + clientEndPoint.Port);

        private static void SetupAdHocHost(WiFiRS9110 wifi, string hostName)

            hostName += DateTime.Now.Ticks.ToString().Substring(0, 3);
            Debug.Print("hostName:" + hostName);
            wifi.StartAdHocHost(hostName, SecurityMode.Open, "", 10);

        // See more at:
        private static void ConfigureIpAddress(WiFiRS9110 wifi, bool doDhcp)

            // the ipAddress is apparently finicky
            if (doDhcp)
                // dynamic means that we need a DHCP server on the LAN
                if (!wifi.NetworkInterface.IsDhcpEnabled)
                // static apparently works well with laptop using MS-Windows as long you use an IP address 169.255.x.x
                wifi.NetworkInterface.EnableStaticIP(IpAddress, SubnetMask, GatewayAddress);
                wifi.NetworkInterface.EnableStaticDns(new string[] { "" });

        private static void wifi_NetworkAddressChanged(object sender, EventArgs e)


        private static void wifi_WirelessConnectivityChanged(object sender, WiFiRS9110.WirelessConnectivityEventArgs e)

            Debug.Print("IsConnected:" + e.IsConnected);
            Debug.Print("NetworkInformation:" + e.NetworkInformation); // this is null   



        private static void AddEventListenersToTheWifiDriver(WiFiRS9110 wifi)

            wifi.NetworkAddressChanged += wifi_NetworkAddressChanged;
            wifi.WirelessConnectivityChanged += wifi_WirelessConnectivityChanged;

        private static void DisconnectTheWifiDriver(WiFiRS9110 wifi)

            if (wifi.IsLinkConnected)

        private static void OpenTheWifiDriver(WiFiRS9110 wifi)

            if (!wifi.IsOpen)
                // open the driver

        private static WiFiRS9110 CreateWifiDriver()

            // the wifi module is MOD2 and we connect via an SPI
            const SPI.SPI_module mod2 = SPI.SPI_module.SPI2;

            // why do we choose 1_10 as the chipset pin? 
            const Cpu.Pin chipSelect = Pin.P1_10;

            // why do we choose 2_11 as the external interupt pin?
            const Cpu.Pin externalInterupt = Pin.P2_11;

            // why do we choose 1_9 as the reset pin?
            const Cpu.Pin reset = Pin.P1_9;

            // why do we choose 4000?
            const uint clockRateKhz = 4000;

            // create a wifi object
            var wifi = new WiFiRS9110(mod2, chipSelect, externalInterupt, reset, clockRateKhz);

            return wifi;

        private static void PrintNetworkInterfaceDetails(WiFiRS9110 wifi)

            Debug.Print("GatewayAddress:" + wifi.NetworkInterface.GatewayAddress);
            Debug.Print("IPAddress:" + wifi.NetworkInterface.IPAddress);
            Debug.Print("SubnetMask:" + wifi.NetworkInterface.SubnetMask);
            foreach (var s in wifi.NetworkInterface.DnsAddresses)
            Debug.Print("IsDhcpEnabled:" + wifi.NetworkInterface.IsDhcpEnabled);
            Debug.Print("IsDynamicDnsEnabled:" + wifi.NetworkInterface.IsDynamicDnsEnabled);
            Debug.Print("NetworkInterfaceType:" + wifi.NetworkInterface.NetworkInterfaceType);

        private static void PrintMethodNameForDebugging(string header)

I have not done adhoc WiFi with a NETMF device…

The first thing I would do is try to ping the Cobra from the PC. This would verify that both are on the same adhoc network and can communicate.

Also, for the listening endpoint, I would use IPAddress.Any.

What messages are appearing in Console window?

1 Like

@ Mike -

Thank you for the reply. My replies follow your quotes.

Fair enough. Do you think it’s a possible way to setup a peer-to-peer socket for data transfer and remote procedure calls?

We have tried pinging and receive a “request timed out” message when we try this IP Address:

[ul]ping # the static ip addres we assigned [/ul]

Does that mean we are not on the same ad-hoc network? Is there another IP address that we could ping?

Sure. We will try that. Maybe this will fix thing ping timeout result.

Do you mean the Debug.Print(…) output of Visual Studio or the console window output after our netsh and ping commands? I can post it all if it’s helpful.

If you can not ping the Cobra from the PC, then an adhoc network has not been established.

I don’t remember seeing any other questions in the past about adhoc. My initial though was adhoc was not implemented. But, I see the method in the Premium documentation.

Maybe someone from GHI can comment on adhoc support, and if it is implemented, provide some sample code.

@ Mike - I will forward this topic on the GHI support. Thanks for your help!