我几天前创建了这个项目,在那里我可以移植一个端口,这样我就可以托管服务器了。 它是如何工作的。
我启动服务器,人们无法加入
我开始申请&我按"打开"
打开端口(请参阅下面的代码)
现在端口已打开,人们可以加入
如果我使用192.16.0.1登录我的路由器并填写名称和密码,然后导航到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 :(得分:0)
它适用于您的事实是因为您的互联网接入点(例如您的路由器)支持IGD Protocol。您使用的NAT库使访问此协议变得更加简单。您的朋友使用的接入点(无线调制解调器,智能手机等)不支持IGD协议或已禁用。