Open a port

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
9 posts Page 1 of 1
Contributors
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

Open a port
clanc789
Hey Coders,

How do I open a port (the specific prot is: 25565) on my computer? I need this to test some application I made which i might releas in the future. +rep for the one who helps me out,
thanks.

Regards,
clanc789
Practice makes perfect!

VIP since: 6-10-2011
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Open a port
MrAksel
25565 is the default port for Minecraft servers, do you plan on making one? :D
Well, to open a port, go to your router's IP address using any web browser, the default IP for Linksys routers is 192.168.1.1. Then login with the router username and password. You might see a 'Games' section and a link to some 'Port mappings' or 'Open ports', thats often the place where you can change port mappings. You would also need to setup a static IP on your computer. So when you have done the above steps, you must select what port to open, and enter the static IP address to your computer that the traffic will be redirected to. If none of this made any sense, check this out http://portforward.com/.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

Re: Open a port
clanc789
I ment, is there no VB.net code to open a port? Oh and yes it is about minecraft :D:D
Practice makes perfect!

VIP since: 6-10-2011
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Open a port
MrAksel
That code may vary from model to model, but if you router supports UPnP (Universal Plug'n'Play) there is some common code to do it.
First open the Add Reference dialog. In the COM tab, find NATUPNP.DLL (NATUPnP Type Library 1.0) and add it to your project.
Then in your code, import the NATUPNPLib namespace and add a variable in you class.
Code: Select all
Private NATClass As UPnPNAT = New UPnPNAT()
To open a port, you can use the following piece of code
Code: Select all
NATClass.StaticPortMappingCollection.Add(25565, "TCP", 25565, "192.168.1.4", true, "Minecraft server")
Replace '192.168.1.4' with the static IP address that you must have set up before using the code. Just search for 'Static IP Address' on Google.
To remove/close a port mapping, use this code
Code: Select all
NATClass.StaticPortMappingCollection.Remove(25565, "TCP")
The full code is now
Code: Select all
Imports NATUPNPLib
Public Class Form1
   Private NATClass As UPnPNat = New UPnPNAT()
   
   Private Sub OpenPort()
     NATClass.StaticPortMappingCollection.Add(25565, "TCP", 25565, "192.168.1.4", true, "Minecraft server") 'The parameters are: External port, Protocol, Internal port, Static IP of your PC, Enabled, Description'
   End Sub

   Private Sub ClosePort()
     NATClass.StaticPortMappingCollection.Remove(25565, "TCP") 'The parameters are: External port, Protocol'
   End Sub

   Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Form1.Load
      MessageBox.Show("Opening port 25565")
      OpenPort()

      MessageBox.Show("Closing port 25565")
      ClosePort()
   End Sub
End Class
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

Re: Open a port
clanc789
Ill look at that tomorrow :D Thx for the help already!
Practice makes perfect!

VIP since: 6-10-2011
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Open a port
mandai
Opening a port is not the same as forwarding it from your router.
If you are using a firewall then you will need to open the port by adding it to a list of exceptions; and this code will be different to above.
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

Re: Open a port
clanc789
And how do I add it to the firewall exceptions. What rights are needed for that?
Practice makes perfect!

VIP since: 6-10-2011
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Open a port
mandai
You can use this to open a port:
Code: Select all
Imports NetFwTypeLib 'COM Component
Code: Select all
    Private Sub btnOpen_Click(sender As System.Object, e As System.EventArgs) Handles btnOpen.Click

        Dim mgrType As Type = Type.GetTypeFromProgID("HNetCfg.FwMgr", True)
        Dim manager As INetFwMgr = Activator.CreateInstance(mgrType)

        Dim portType As Type = Type.GetTypeFromProgID("HNetCfg.FwOpenPort", True)
        Dim port As INetFwOpenPort = Activator.CreateInstance(portType)

        port.Name = "test"
        port.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP
        port.Port = 1234

        port.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL
        'port.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V4 'default

        manager.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(port)

    End Sub
You will need admin rights in order for this to work.
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

Re: Open a port
clanc789
Thanks mandai! Ill lock the topic now since its solved.
Practice makes perfect!

VIP since: 6-10-2011
9 posts Page 1 of 1
Return to “Coding Help & Support”