【发布时间】:2017-09-23 14:12:11
【问题描述】:
几天前我创建了这个项目,我可以在其中移植一个端口,以便托管一个服务器。 它是如何工作的。
我启动了服务器,人们无法加入
我启动应用程序并按“打开”
它打开了端口(见下面的代码)
现在端口已经开放,人们可以加入了
如果我使用 192.16.0.1 登录我的路由器并填写名称和密码,然后导航到 portforward 选项卡,我可以同时执行确切的结果。 这是主要问题。我的朋友在蜂窝网络上运行,换句话说,他正在使用移动网络运行。我认为它被称为一个
移动宽带
当他尝试移植时,它根本没有做任何事情。但是当我在家中的所有 PC 上运行它时,它就像一个魅力。
我现在面临的问题是,什么可能导致他无法使用此应用程序,我的选择是什么?我应该尝试不同的库吗?
我目前正在使用 Mono.NAT
https://www.fluxbytes.com/csharp/upnp-port-forwarding-the-easy-way/
代码
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.");
}
}
【问题讨论】:
-
为什么要在新线程上调用 Clipboard.SetText?
-
因为否则它会给我一个非常奇怪的错误。随便调试一下看看是什么,我想不出圆顶的错误。
标签: c# networking router portforwarding