首页 > 解决方案 > NanoFramework 上的套接字连接

问题描述

我遇到了一个问题,我无法使用以下代码 Telnet 到我的 Nucleo-F746ZG:

我看到 DHCP 分配了一个有效的 IP 地址,这意味着板子可以看到网络(和路由器),但是当我使用 Telnet 客户端连接时。代码永远不会离开 listener.Accept。相同的代码在 .NET Framework 下的 Windows 上运行良好。

        private void ConnectionListenerThread()
    {
        byte [] buff = new byte[ 1024 ];
        int received;

        //
        // Grab a collection of available network interfaces.
        //
        NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();

        //
        // We need at least one interface to continue.
        //
        if ( nis.Length > 0 )
        {
            NetworkInterface ni = nis[0];
            ni.EnableDhcp();

            //
            // Wait to be assigned an IP address.
            //
            while ( ni.IPv4Address == null || ni.IPv4Address.Length == 0 || ni.IPv4Address.Equals( "0.0.0.0" ) )
            {
                Thread.Sleep( 100 );
            }

            Debug.WriteLine( $"DHCP has given us the address {ni.IPv4Address},{ni.IPv4SubnetMask},{ni.IPv4GatewayAddress}" );

            while ( true )
            {
                _clientSocket = null;

                //
                // We now have an IP address allocated so we can await
                // incoming client connections.
                //
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, ListeningPort);
                Socket listener = new Socket( localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp );

                try
                {
                    listener.Bind( localEndPoint );
                    listener.Listen( 100 );

                    Debug.WriteLine( $"Waiting for an incoming connection on port {ListeningPort}.." );
                    _clientSocket = listener.Accept();
                    Debug.WriteLine( "Connected to a client" );

                    while ( true )
                    {
                        received = _clientSocket.Receive( buff );
                        if ( received > 0 )
                        {
                            OnSerialDataReceived?.Invoke( this, new EventArguments.BytesReceivedEventArgs( buff, received ) );
                        }
                    }
                }
                catch ( Exception ex )
                {
                    Debug.WriteLine( $"Got a network exception: {ex.Message}" );
                }
            }
        }
    }

ListeningPort 通常设置为 54321。

这是显示已安装 NanoFramework 版本的系统信息。

System Information
HAL build info: nanoCLR running @ ST_NUCLEO144_F746ZG
  Target:   ST_NUCLEO144_F746ZG
  Platform: STM32F7

Firmware build Info:
  Date:     Jul 13 2020
  Type:     MinSizeRel build with ChibiOS v19.1.4.1
  Version:  1.4.590.0
  Compiler: GNU ARM GCC v9.3.1

有人对我做错了什么有任何建议吗?

安迪

标签: nanoframework

解决方案


这看起来像一个错误......请在我们的 GitHub 中提出问题。确保添加一个重现问题的项目。


推荐阅读