Search code examples
c#socketstcpserver.net-micro-framework

WSAENETDOWN SocketException on a FEZ Spider kit


I'm trying to write a TCP server in .NETMF 4.3 for my FEZ Spider kit.

public partial class Program
{
    void ProgramStarted()
    {
        ethernetJ11D.NetworkInterface.Open();
        ethernetJ11D.UseStaticIP("192.168.0.8", "255.255.255.0", "192.168.0.1");
        Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        EndPoint endPoint = new IPEndPoint(IPAddress.Any, 7777);
        serverSocket.Bind(endPoint);
        serverSocket.Listen(10);

        new Thread(() =>
        {
            while (true)
            {
                Debug.Print("Network up: " + ethernetJ11D.IsNetworkUp);//true
                Debug.Print("Network connected: " + ethernetJ11D.IsNetworkConnected);//true
                System.Net.Sockets.Socket clientSocket = serverSocket.Accept();//exception!
                new Thread(new Request(clientSocket).Process);
            }
        }).Start();
    }

The serverSocket.Accept method throws a SocketException with error code 10050. This page says it's WSAENETDOWN: Network is down. However the ethernetJ11D's properties state that the connection is up and I can ping the device without any problems. What am I doing wrong?

EDIT

When I tried running the following client I got the same exception on the socket.Connect call.

ethernetJ11D.NetworkInterface.Open();
ethernetJ11D.UseStaticIP("192.168.0.8", "255.255.255.0", "192.168.0.1");
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("192.168.0.5"), 7777);
socket.Connect(endpoint);

Debug.Print("connected");
byte[] msg = new byte[] { 7, 77, 222 };
socket.Send(msg);

EDIT 2

I've tried to implement the UDP client instead. It doesn't work either.


Solution

  • I've solved the problem:

    1. Change IPAddress.Any to a static IP,e.g. IPAddress.Parse("192.168.0.8").
    2. Delay Bind by adding Thread.Sleep(3000) right before it. It seems that the interface's network settings configuration takes some time.