FEZ Spider工具包上的WSAENETDOWN SocketException

时间:2015-01-28 12:18:46

标签: c# sockets tcpserver .net-micro-framework

我正在尝试在.NETMF 4.3中为我的FEZ Spider工具包编写一个TCP服务器。

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();
    }

serverSocket.Accept方法抛出一个错误代码为10050的SocketException。This页面说它是WSAENETDOWN:网络已关闭。但是,ethernetJ11D的属性表明连接已启动,我可以毫无问题地ping设备。我做错了什么?

修改

当我尝试运行以下客户端时,我在socket.Connect调用上得到了相同的异常。

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);

编辑2

我试图改为实现UDP客户端。它也不起作用。

1 个答案:

答案 0 :(得分:2)

我已经解决了这个问题:

  1. IPAddress.Any更改为静态IP,例如。 IPAddress.Parse("192.168.0.8")
  2. 延迟绑定,在其前面添加Thread.Sleep(3000)。看来界面的网络设置配置需要一些时间。