使用UPnP转发端口-关闭端口

人气:458 发布:2022-10-16 标签: delphi port delphi-7

问题描述

我正在使用Port Forwarding by Using "HNetCfg.NATUPnP" Ole Object Failed中的代码进行端口转发,工作正常,只是在应用程序终止时无法关闭端口。

AddUPnPEntry(1234, 'Hello3', '192.168.1.34');
尽管我重启了我的电脑,但1234端口仍然打开,我在canyouseame.org上测试了它。 那么,我如何关闭该端口?

编辑:已解决,我只需要重新启动(关闭并打开)我的路由器以再次关闭端口。

推荐答案

AddUPnPEntry()使用IStaticPortMappingCollection.Add()方法。有关联的IStaticPortMappingCollection.Remove()方法,例如:

Procedure RemoveUPnPEntry(Port: Integer);
Var
  Nat: Variant;
  Ports: Variant;
Begin
  try
    Nat := CreateOleObject('HNetCfg.NATUPnP');
    Ports := Nat.StaticPortMappingCollection;
    Ports.Remove(Port, 'TCP');
  except
    ShowMessage('An Error occured with removing UPnP Ports. ' +
      'Please check to see if your router supports UPnP and ' +
      'has it enabled or disable UPnP.');
  end;
End;

RemoveUPnPEntry(1234);

570