Socket communication to PC

Did someone try to communicate with PC over sockets?
I try to build Windows form application and connect it to spider, but having some problems. If someone already do it i will be happy if that somebody share some code.
Socket don’t have any events (DataAvailable) and i am getting: "No connection could be made because the target machine actively refused it 192.168.1.55:1721"
Spider (server) code is:


        public void InitServer()
        {
            if (Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces().Length < 1)
            {
                Debug.Print("No Active network interfaces. ");
            }
            else
            {

                Microsoft.SPOT.Net.NetworkInformation.NetworkInterface NI = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];

                NI.EnableStaticIP(cIP, cSubnetMask, cGateway);
                IPAddress hostIP = IPAddress.Parse(NI.IPAddress);

                //WEB
                _WebSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint epWeb = new IPEndPoint(hostIP, cHttpPort);
                _WebSocket.Bind(epWeb);
                _WebSocket.Listen(1);

                //PC
                _PCSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint epPC = new IPEndPoint(hostIP, cHttpPort);
                _PCSocket.Bind(epPC);
                _PCSocket.Listen(1);
                
                Thread threadWeb = new Thread(ListenWeb);
                threadWeb.Start();
                Thread threadPC = new Thread(ListenPC);
                threadPC.Start();
            }
        }



        private void ListenWeb()
        {
            Debug.Print("listening...");
            while (true)
            {
                try
                {
                    Socket newSock = _PCSocket.Accept();
                    Debug.Print("Accepted a connection from " + newSock.RemoteEndPoint.ToString());
                    byte[] messageBytes = Encoding.UTF8.GetBytes("Test");
                    newSock.Send(messageBytes);
                    newSock.Close();
                }
                catch (Exception e)
                {
                    Debug.Print(e.Message);
                }
            }
        }

        private void ListenPC()
        {
            Debug.Print("listening...");
            while (true)
            {
                try
                {
                    Socket newSock = _WebSocket.Accept();

My mistake. I am sorry for “spam”.

Socket newSock = _PCSocket.Accept();

should be

Socket newSock = _WEBSocket.Accept();

What framework version are you running? If you are running 4.2, there is a new method for networking (Wiki will be updated shortly to reflect this method). Here is an example of using the WiFiRS9110 on the Cobra 2.


static WiFiRS9110 Interface;
Interface = new WiFiRS9110(SPI.SPI_module.SPI2, G120.Pin.P1_10, G120.Pin.P2_11, G120.Pin.P1_9);
            Interface.Open();

//Enable DHCP
            if (!Interface.NetworkInterface.IsDhcpEnabled)
                Interface.NetworkInterface.EnableDhcp();

//Assign the TCP/IP stack
            NetworkInterfaceExtension.AssignNetworkingStackTo(Interface);

//Setup our events
            Interface.NetworkAddressChanged += new GHI.Premium.Net.NetworkInterfaceExtension.NetworkAddressChangedEventHandler(GHI_Wifi_NetworkAddressChanged);
            Interface.WirelessConnectivityChanged += new GHI.Premium.Net.WiFiRS9110.WirelessConnectivityChangedEventHandler(GHI_Wifi_WirelessConnectivityChanged);

           //Scan for networks
            WiFiNetworkInfo[] wifiInfo = Interface.Scan();

            bool found = false;

            for (int i = 0; i < wifiInfo.Length; i++)
            {
                if (wifiInfo[i].SSID == "linksys")
                {
                    found = true;
                    Interface.Join(wifiInfo[i], "password");
                    Debug.Print(wifiInfo[i].RSSI.ToString());
                    break;
                }
            }

            if (!found)
                Debug.Print("could not find network");

            while (true)
            {

                IPAddress ip = IPAddress.GetDefaultLocalAddress();

                if (ip != IPAddress.Any) break;

                Thread.Sleep(1000);
            }

Also, both of the sockets you have created are bound to the same port, which by itself can cause some issues.

True, already fix this. Sometimes copy/paste really suck.

I am using Spider starter kit, so wired network not WIFI.

@ Makla - what ethernet module are you using? (ENC28 or J11D)

Don’t bother. Everything works now. It was just a little mistype.
I have J11D.