2013-09-26 19 views
0

我試圖打開端口,使用UPnP做到這一點我使用mono.nat無效NewPortMappingIndex使用Mono.nat

這是我的代碼,它的簡單,應該工作,但它會拋出「錯誤402 :無效的NewPortMappingIndex「上的每個。 「設備」的各種其他功能像GetExternalIP一樣工作。

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    AddHandler NatUtility.DeviceFound, AddressOf DeviceFound 
    NatUtility.StartDiscovery() 
End Sub 

Private Sub DeviceFound(ByVal sender As Object, ByVal e As Mono.Nat.DeviceEventArgs) 
    Debug.WriteLine("Found") 
    Dim device As INatDevice = e.Device 
    Debug.WriteLine(device.GetHashCode) 
    For i = 0 To device.GetAllMappings.Length - 1 
     Dim _PortMap As Mono.Nat.Mapping = device.GetAllMappings(i) 
     ListBox1.Items.Add(_PortMap.Description & " | " & _PortMap.PrivatePort & " | " & _PortMap.Protocol & " | " & _PortMap.PublicPort) 
    Next 

End Sub 

我也嘗試關閉防火牆和任何其他可能會干擾的事情。我也知道我的路由器啓用了UPNP。

回答

0

這似乎是一次循環的次數太多:

對於i = 0到device.GetAllMappings.Length

+0

哦哎呀,我也有一個 - 1之上,但把它關閉調試。所以那不是問題。不管怎麼說,還是要謝謝你。我現在要解決這個問題,以便其他人不會犯同樣的錯誤。即使我做'Debug.WriteLine(device.GetAllMappings.Length)'我得到相同的錯誤。 –

+0

那個電話應該怎麼工作?你似乎使用它來獲得所有的映射(僅用於循環計數器),然後使用具有索引參數的相同函數 - 這是一些vb.net語法,我不明白嗎?我會假設GetAllMappings()返回一個可以使用的映射數組,或者如果您一次只想獲得一個映射,您可以使用GetSpecificMapping(index)... – jku

+0

GetAllMappings返回一個數組,爲什麼我得到它的循環長度。它可以簡單地爲每個_PortMap –