c#UPnP似乎不起作用?

时间:2016-05-15 10:49:40

标签: c# udp p2p upnp windows-firewall

我有一个ac#peer-2-peer项目,我的应用程序应该在用户设备中配置UPnP以打开端口并监听UDP数据报,我使用NATUPnP.dll Com来组建新的端点如下:

NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
NATUPNPLib.IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;
mappings.Add(20000, "UDP", 20000, "192.168.8.102", true, "Local Web Server");
foreach (NATUPNPLib.IStaticPortMapping portMapping in mappings)
{
    Console.WriteLine(portMapping.Description); //gives me "Local Web Server"
    Console.WriteLine(portMapping.ExternalIPAddress); // gives me "10.178.196.123"
    Console.WriteLine(portMapping.Enabled); // gives me "True"
}
Console.ReadKey();

在此之后,我用一个简单的UDP客户端编写一个简单的UDP监听器来测试我是否成功映射了新的端点(port forwarding)和 监听器的代码< / strong>是:

public static int Main()
{
    bool done = false;
    UdpClient listener = new UdpClient(listenPort);
    IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
    string received_data;
    byte[] receive_byte_array;
    try
    {
        while (!done)
        {
            Console.WriteLine("Waiting for broadcast");
            receive_byte_array = listener.Receive(ref groupEP);
            Console.WriteLine("Received a broadcast from {0}", groupEP.ToString());
            received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
            Console.WriteLine("data follows \n{0}\n\n", received_data);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
    listener.Close();
    return 0;
}

客户

UdpClient udpSender = new UdpClient();
IPEndPoint localServerGateway = new IPEndPoint(IPAddress.Parse("10.178.196.123"), 20000);
string message = "testmessage";
byte[] messageBytes = Encoding.ASCII.GetBytes(message);
try
{
    udpSender.Send(messageBytes, messageBytes.Length, localServerGateway);
}
catch (Exception error)
{
    Console.WriteLine("Error while sending message: " + error.ToString());
}
udpSender.Close();

当我向环回地址(127.0.0.1)发送测试消息时,服务器控制台向我显示此消息并且每件事情都正常,但是当我向外部IP地址发送测试消息(10.178.196.123)时没有显示,所以我的问题是:我的代码有问题吗?如果不是为什么测试消息没有被服务器收到?这种方法是最合适的p2p应用方法(在我的情况下,打孔不是一种选择)?

映射端口并运行服务器(使用Netstat)后,这是我的连接:

Proto  Local Address          Foreign Address        State
TCP    0.0.0.0:135            0.0.0.0:0              LISTENING
TCP    0.0.0.0:445            0.0.0.0:0              LISTENING
TCP    0.0.0.0:1025           0.0.0.0:0              LISTENING
TCP    0.0.0.0:1026           0.0.0.0:0              LISTENING
TCP    0.0.0.0:1027           0.0.0.0:0              LISTENING
TCP    0.0.0.0:1028           0.0.0.0:0              LISTENING
TCP    0.0.0.0:1029           0.0.0.0:0              LISTENING
TCP    0.0.0.0:2869           0.0.0.0:0              LISTENING
TCP    0.0.0.0:5357           0.0.0.0:0              LISTENING
TCP    127.0.0.1:1242         127.0.0.1:4823         ESTABLISH
TCP    127.0.0.1:1244         127.0.0.1:4836         ESTABLISH
TCP    127.0.0.1:4823         127.0.0.1:1242         ESTABLISH
TCP    127.0.0.1:4836         127.0.0.1:1244         ESTABLISH
TCP    192.168.8.102:139      0.0.0.0:0              LISTENING
TCP    192.168.8.102:2869     192.168.8.1:51156      TIME_WAIT
TCP    192.168.8.102:2869     192.168.8.1:51157      TIME_WAIT
TCP    192.168.8.102:2869     192.168.8.1:51158      TIME_WAIT
TCP    192.168.8.102:4683     198.252.206.25:443     ESTABLISH
TCP    192.168.8.102:4824     104.16.35.249:80       ESTABLISH
TCP    192.168.8.102:4831     172.217.19.238:443     ESTABLISH
TCP    192.168.8.102:4832     94.142.38.38:443       ESTABLISH
TCP    192.168.8.102:4835     94.142.38.53:443       ESTABLISH
TCP    192.168.8.102:4837     104.16.33.249:80       ESTABLISH
TCP    192.168.8.102:4839     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4840     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4841     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4842     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4843     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4844     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4845     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4846     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4847     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4848     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4849     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4850     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4851     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4852     192.168.8.1:50473      TIME_WAIT
TCP    192.168.8.102:4853     192.168.8.1:50473      TIME_WAIT
TCP    [::]:135               [::]:0                 LISTENING
TCP    [::]:445               [::]:0                 LISTENING
TCP    [::]:1025              [::]:0                 LISTENING
TCP    [::]:1026              [::]:0                 LISTENING
TCP    [::]:1027              [::]:0                 LISTENING
TCP    [::]:1028              [::]:0                 LISTENING
TCP    [::]:1029              [::]:0                 LISTENING
TCP    [::]:2869              [::]:0                 LISTENING
TCP    [::]:5357              [::]:0                 LISTENING
UDP    0.0.0.0:3702           *:*
UDP    0.0.0.0:3702           *:*
UDP    0.0.0.0:3702           *:*
UDP    0.0.0.0:3702           *:*
UDP    0.0.0.0:5355           *:*
UDP    0.0.0.0:20000          *:*
UDP    0.0.0.0:52766          *:*
UDP    0.0.0.0:55825          *:*
UDP    127.0.0.1:1900         *:*
UDP    127.0.0.1:63074        *:*
UDP    192.168.8.102:137      *:*
UDP    192.168.8.102:138      *:*
UDP    192.168.8.102:1900     *:*
UDP    192.168.8.102:63073    *:*
UDP    [::]:3702              *:*
UDP    [::]:3702              *:*
UDP    [::]:3702              *:*
UDP    [::]:3702              *:*
UDP    [::]:5355              *:*
UDP    [::]:52767             *:*
UDP    [::]:55826             *:*
UDP    [::1]:1900             *:*
UDP    [::1]:63072            *:*
UDP    [fe80::bc13:29c3:a8c3:c74a%16]:546  *:*
UDP    [fe80::bc13:29c3:a8c3:c74a%16]:1900  *:*
UDP    [fe80::bc13:29c3:a8c3:c74a%16]:63071  *:*

0 个答案:

没有答案