几天前,我创建了这个项目,在那里我可以转发一个端口,这样我就可以托管一个服务器。它是如何工作的。
如果我用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.");
}
}发布于 2017-09-24 10:46:48
它为您工作的事实是,您的互联网接入点(例如,您的路由器)支持IGD协议。您使用的NAT库只会使访问该协议变得更简单。您的朋友使用接入点(无线调制解调器、智能手机等)它要么不支持IGD协议,要么禁用它。
https://stackoverflow.com/questions/46380491
复制相似问题