1

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.

Community
  • 1
  • 1
user1564858
  • 47
  • 1
  • 6
  • What *exactly* do you mean by 'port is still open after restart' ? Have you tried connecting to the port? Did you check using netstat -ano (or similar) whether there's an actual process listening on the port? – Frank Schmitt Feb 21 '14 at 09:13
  • @Frank I mean the port is still forwarded, i tried it on canyouseeme.org "Your ISP is not blocking port 1234", yes someone can still connect to me using port 1234 i use my IdTCPServer1, its my chat application – user1564858 Feb 21 '14 at 09:31
  • If I understand the output of canyouseeme.org correctly, it just tells you whether there's a firewall blocking access to port 1234 between them and your computer. It doesn't tell you whether there's an actual program running on port 1234. Please try connecting to the port (e.g. using telnet), and check the output of netstat. – Frank Schmitt Feb 21 '14 at 09:43
  • 1
    restarting your router is not a solution for customers. It may work for testing but is not a long term good solution. – Robert Love Feb 21 '14 at 16:19
  • @user1564858, did you really made ir work ??? How? – NaN May 22 '14 at 21:42

1 Answers1

1

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);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • It always retrieve an error telling that could not close port because maybe it does not have upnp enabled, but when I open it, no errors occur. – NaN May 22 '14 at 17:50
  • Take it up with Microsoft, since `HNetCfg.NATUPnP` is Microsoft's implementation of UPnP. If `Add()` works but `Remove()` fails, then `Remove()` probably has a bug in it. – Remy Lebeau May 22 '14 at 18:50