uPnP和.Net错误

时间:2015-03-24 20:48:12

标签: .net vb.net upnp

我正在尝试创建一个程序,将端口映射到我的uPnP路由器,因为我的端口经常被重置,我无法连接到我家。我创建(更像是复制)以下代码,单击按钮时总是出错。

代码:

Imports NATUPNPLib

Public Class Form1
    Dim entry As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
    Dim upnp As New NATUPNPLib.UPnPNAT
    Dim portmaps As NATUPNPLib.IStaticPortMappingCollection = upnp.StaticPortMappingCollection

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        portmaps.Add("5901", "TCP", "5901", "10.175.3.22", True, "VNC")
    End Sub
End Class

错误:

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Runtime.InteropServices.COMException (0x80040208): A user-supplied component or subscriber raised an exception (Exception from HRESULT: 0x80040208)
   at NATUPNPLib.IStaticPortMappingCollection.Add(Int32 lExternalPort, String bstrProtocol, Int32 lInternalPort, String bstrInternalClient, Boolean bEnabled, String bstrDescription)
   at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

由于我不是最熟练的.NET,有没有人有任何建议?

1 个答案:

答案 0 :(得分:0)

在我看来,每次用户点击按钮时,您只需添加相同的端口/ IP。如果用户多次单击该按钮,则可能会导致异常。

在调用portmaps.Add(...)之前,您可能希望查看端口5901是否已在端口映射中。