I'm using the code from Port Forwarding by Using "HNetCfg.NATUPnP" Ole Object Failed for port forwarding, works fine, except that I can't close the port when the application is terminated.

AddUPnPEntry(1234, 'Hello3', '192.168.1.34');

1234 port is still open even thoug I restarted my pc, I tested it on canyouseeme.org. So how can I close the port ?

EDIT: Solved, I just need to restart(turn off and on) my router to close the port again.

有帮助吗?

解决方案

AddUPnPEntry() uses the IStaticPortMappingCollection.Add() method. There is an associated IStaticPortMappingCollection.Remove() method, eg:

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top