Windows 10 IoT中的ConnectionRefused

时间:2019-04-05 15:36:35

标签: c# tcp uwp windows-10-iot-core dhcp

我创建了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)
   {
       ... ...

2 个答案:

答案 0 :(得分:0)

首先,您需要检查是否已添加功能 Private Networks (Client & Server) ,该功能提供通过防火墙对家庭和工作网络的入站和出站访问。请参阅此文档(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

答案 1 :(得分:0)

  

来自@Michale Xu-MSFT的评论

@Nicolò,我对MSDN forum给出了类似问题的答复。