Can't Connect from Panda/Wiznet to PC

After successfully working through examples on “The Internet of Things,” I’m attempting to connect a Panda II w/ Wiznet running as a client to a server program running on a PC but getting a timeout exception when calling socket.Connect(). The server on the PC uses System.Net.Sockets and the client on the Panda uses GHIElectronics.NETMF.Net.Sockets. Is there a problem using these two different sockets? The server instantiates a (.NET) TcpListener and calls listener.AcceptTcpClient() while the client instantiates a (GHI) Socket and calls socket.Connect(serverEndPoint). The timeout exception results. Both the server and client code were successfully tested on the same PC, both using System.Net.Sockets and then I attempted to port the client to the Panda and MF. Any ideas of why the client fails to connect? Relevant code follows and exeption report.

Client code:


using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.Sockets;
using GHIElectronics.NETMF.Net.NetworkInformation;

namespace MFTestConnection
{
    public class Program
    {
        const int cServerIP = 64;
        const int cPort = 2000;
        const byte cMicroControllerIP = 222;

        static Socket socket;

        public static void Main()
        {
            byte[] ip = { 192, 168, 1, cMicroControllerIP };
            byte[] subnet = { 255, 255, 255, 0 };
            byte[] gateway = { 192, 168, 1, 254 };
            byte[] mac = { 0x00, 0x88, 0x98, 0x90, 0xD4, 0xE0 };
            try
            {
                WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di7, true);
                NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
                NetworkInterface.EnableStaticDns(new byte[] { 192, 168, 1, 254 });
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
            }

            bool connectedToDataServer = false;
            IPEndPoint serverEndPoint = new IPEndPoint(new IPAddress(new byte[] { 192, 168, 1, cServerIP }), cPort);
            Debug.Print("Connecting to server...");
            do
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    socket.Connect(serverEndPoint);
                }
                catch (Exception e)
                {
                    Debug.Print(e.Message);
                    socket.Close();
                    Debug.Print("waiting 5 seconds...");
                    Thread.Sleep(5000);
                }
            } while (!connectedToDataServer);
        }
    }
}

Server code:


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace PBRDataServer
{
    class Program
    {
        const int       cDataServerIP   = 64;
        const string    cPBRDataFile    = "PBRData.dat";
        const int       cDataPort       = 2000;  // must match port in DataStore class of MFPBR app
        const int       cBufSize        = 256;
        const int       cReadWait       = 1000;
        const string    cHandshake      = "DataHandshake";  // must match handshake string in DataStore class of MFPBR app
        const int       cReconnectWait  = 5000;

        public static void Main()
        {
            IPAddress serverIP = new IPAddress(new byte[] { 192, 168, 1, cDataServerIP });
            StreamWriter txtDataOut = new StreamWriter(@ cPBRDataFile);
            TcpListener listener = new TcpListener(serverIP, cDataPort);
            TcpClient client;
            byte[] buf = new byte[cBufSize];
            int bytesRead = 0;
            bool dataAvailable = false;

            listener.Start();
            Console.WriteLine("Data server started, listening to port {0}", cDataPort);
            bool connectedToDataStore = false;
            do {
                client = listener.AcceptTcpClient();  // wait here until client connects
                Console.WriteLine("Connected to client {0}", client.Client.RemoteEndPoint);
                . 
                .
                .
  

Client Output:

Connecting to server…
#### Exception System.Exception - 0x00000000 (1) ####
#### Message: Timeout occurs during connection establishment

#### GHIElectronics.NETMF.Net.SocketNative::connect [IP: 0135] ####
#### GHIElectronics.NETMF.Net.Sockets.Socket::Connect [IP: 002c] ####
#### MFTestConnection.Program::Main [IP: 00a7] ####

A first chance exception of type ‘System.Exception’ occurred in GHIElectronics.NETMF.W5100.dll
Timeout occurs during connection establishment

waiting 5 seconds…
The program ‘[2] Micro Framework application: Managed’ has exited with code 0 (0x0).

In the server

Remove

IPAddress serverIP = new IPAddress(new byte[] { 192, 168, 1, cDataServerIP }); 

and replace

TcpListener listener = new TcpListener(serverIP, cDataPort);

with

TcpListener listener = new TcpListener(cDataPort);

Also

Make sure you can ping the Panda from the PC.

Also, make sure your firewall allows the server port to accept TCP calls.

Hi Mike. Changed the code as suggested (despite warnings that TcpListener(int DataPort) is obsolete and “deprecated”) and still the same exception. I remember successfully pinging the Panda before but will try it again to be sure.

Pinging the Panda is perfect.

When i tested the client on the same machine i had a warning from the Windows firewall and gave permission. Haven’t heard boo since.

So the problem is fixed? ;D

Sorry, i wasn’t clear–I didn’t think it was fixed but then I went and turned the firewall off just to be sure and yes, it IS fixed–with the firewall off it does connect. Although the firewall was set to ask me when a program attempted to connect, and although it asked me before, this time it wasn’t asking. Thanks, Mike.

Actually, the same timeout exception occurs the first time it tries it but then, the code waits five seconds and tries again and it connects the second time, consistently. I can live with that but it is a bit annoying and i wonder why it times out the first attempt.