2017-09-23 111 views
0

我幾天前創建了這個項目,我可以在這裏端口端口,這樣我就可以託管一臺服務器。 它是如何工作的。如何正確地前移端口以及我的選項是什麼?

  1. 我啓動服務器和人民不能參加

  2. 我啓動應用程序&我按下 「打開」

  3. 它打開的端口(見下面的代碼)

  4. 現在港口是開放的,人們可以加入


如果我要用192.16.0.1登錄到我的路由器並填寫名稱和密碼,然後導航到portforward選項卡,我可以在確切結果的同時進行操作。 這是主要問題。我的朋友正在蜂窩網上奔跑,換句話說他正在用移動網絡運行。我認爲,它被稱爲

移動寬帶

當他試圖portforward它沒有做任何事情。但是當我在家中的所有電腦上運行它時,它的作用就像一個魅力。

我現在面臨的問題是,什麼可能導致他無法使用此應用程序,我有什麼選擇?我應該嘗試不同的圖書館嗎?

我目前使用Mono.NAT

https://www.fluxbytes.com/csharp/upnp-port-forwarding-the-easy-way/

CODE

private bool btnOpenWasClicked = false; 
     private bool btnCloseWasClicked = false; 
     [STAThread] 

     private void btnOpen_Click(object sender, EventArgs e) 
     { 
      btnOpenWasClicked = true; 
      NatUtility.DeviceFound += DeviceFound; 
      NatUtility.StartDiscovery(); 

     } 


private void DeviceFound(object sender, DeviceEventArgs args) 
     { 
      if (btnOpenWasClicked == true) 
      { 
       INatDevice device = args.Device; 
       Mapping minecraftTCP = new Mapping(Protocol.Tcp, 25565, 25565); 
       Mapping minecraftUDP = new Mapping(Protocol.Udp, 25565, 25565); 
       minecraftTCP.Description = "MinecraftTCP"; 
       minecraftUDP.Description = "MinecraftUDP"; 
       device.CreatePortMap(minecraftTCP); 
       device.CreatePortMap(minecraftUDP); 

       foreach (Mapping portMap in device.GetAllMappings()) 
       { 
        Debug.Print(portMap.ToString()); 
       } 

       MessageBox.Show("Port 25565 has been opened."); 
       DialogResult diag = MessageBox.Show("This is the IP you will give to your friends: " + device.GetExternalIP().ToString() + ":25565" + " Do you wanna copy the IP? ", 
        "Success", MessageBoxButtons.YesNo, MessageBoxIcon.Information); 
       if (diag == DialogResult.Yes) 
       { 
        Thread thread = new Thread(() => Clipboard.SetText(device.GetExternalIP() + ":25565")); 
        thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA 
        thread.Start(); 
        thread.Join(); //Wait for the thread to end 
       } 
      } 

      if (btnCloseWasClicked == true) 
      { 
       INatDevice device = args.Device; 
       device.DeletePortMap(new Mapping(Protocol.Tcp, 25565, 25565)); 
       device.DeletePortMap(new Mapping(Protocol.Udp, 25565, 25565)); 
       MessageBox.Show("Port closed."); 
      } 
     } 
+0

你爲什麼在一個新的線程上調用Clipboard.SetText? –

+0

因爲否則它會引發一個非常奇怪的錯誤。爲了調試它並檢查它是什麼,我不能想到圓頂頂部的錯誤。 –

回答

0

,它爲你工作的事實,因爲你的互聯網接入點(如路由器)支持IGD Protocol 。您使用的NAT庫只是簡化了訪問此協議。您的朋友使用的接入點(無線調制解調器,智能手機等)不支持IGD協議或禁用它。

相關問題