windows 10 个 IoT 中的 ConnectionRefused

ConnectionRefused in windows 10 IoT

我已经创建了一个 DHCP 服务器并添加了一个 TCP 服务器。当我在 DHCP 服务器的地址 IPv4 发送消息时,客户端回答:ConnectionRefused.

我已将 属性 设置为 true

DataSocket = new DatagramSocket();
DataSocket.Control.MulticastOnly = true;

此代码 运行 应用 UWP 的启动:

private void Start()
{
  if (this.StatusButton.Content.ToString().Equals(AVVIA))
  {
    this.ViewModel.Action = "AVVIO DEL SERVER DHCP IN CORSO...";
    RunServer();
    this.StatusButton.Content = UPDATE;
    Task.Run(async () => 
    {
      await Task.Delay(DELAY);
      Server = new Server();
      Server.StartServer();
     });
  }

  ... ... ...

private void RunServer()
{
  IPAddress iPAddress;

  DhcpServer = new ScaemDhcp.DhcpServer();
  iPAddress = new IPAddress(new byte[] { 192, 168, 1, 101 });
  DhcpServer.Run(iPAddress, DNS, SUB_MASK, SERVER_IDENTIFIER, ROUTER_IP);
}
public void Run(IPAddress iPAddress, string dns, string subnetMask, string serverIdentifier, string routerIP)
{
  var server = new Dhcp(iPAddress);
  server.ServerName = dns;
  server.BroadcastAddress = IPAddress.Broadcast.ToString();
  server.OnDataReceived += (sender, dhcpRequest) =>
  {
    try
    {
      var type = dhcpRequest.GetMsgType();
      var ip = iPAddress;
      var replyOptions = new DhcpReplyOptions();
      replyOptions.SubnetMask = IPAddress.Parse(subnetMask);
      replyOptions.DomainName = server.ServerName;
      replyOptions.ServerIdentifier = IPAddress.Parse(serverIdentifier);
      replyOptions.RouterIP = IPAddress.Parse(routerIP);
      replyOptions.DomainNameServers = new IPAddress[]
      {IPAddress.Parse("8.8.8.8"), IPAddress.Parse("8.8.4.4")};

      if (type == DhcpMsgType.DHCPDISCOVER)
      {
        dhcpRequest.SendDHCPReply(DhcpMsgType.DHCPOFFER, ip, replyOptions);
      }
      if (type == DhcpMsgType.DHCPREQUEST)
      {
        dhcpRequest.SendDHCPReply(DhcpMsgType.DHCPACK, ip, replyOptions);
      }
    }
    catch (Exception e)
    { }
 };
 server.Start();
}

... ...

public async void StartServer()
{
  try
  {
    var streamSocketListener = new StreamSocketListener();

    streamSocketListener.ConnectionReceived += this.StreamSocketListener_ConnectionReceived;

    await streamSocketListener.BindServiceNameAsync(PORT.ToString());
   }
   catch (Exception ex)
   {
       ... ...

首先,您需要检查是否添加了能力Private Networks (Client & Server), this capability provides inbound and outbound access to home and work networks through the firewall. Please refer to this document(enter link description here).

然后,您需要检查端口是否可用,您可以在防火墙中添加端口 5037 的规则,请使用以下命令尝试。

netsh advfirewall firewall add rule name="5037 In" dir=in protocol=TCP localport=5037 action=Allow
netsh advfirewall firewall add rule name="5037 Out" dir=out protocol=TCP localport=5037 action=Allow

From comment by @Michale Xu - MSFT

@Nicolò,我已经在 MSDN forum 上回复了类似的问题。