c# UPnP not seems to be work?

笑着哭i 提交于 2019-12-10 11:42:59

问题


I have a c# peer-2-peer project where my application should configure UPnP in users devices to open a port and listen to UDP datagrams , I used NATUPnP.dll Com assemply to map a new Endpoints as the following :

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();

after this I write a simple UDP listener with a simple UDP client to test whether I succeed on mapping new End points ( port forwarding it) and the code for Listener was :

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;
}

and the Client :

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();

when I send a test message to the loopback address (127.0.0.1) the server console show me this message and every thing works fine , but when I send a test message to the external ip address (10.178.196.123) nothing show , so my question is : is there any problem in my code ? and if not why test message not recieved by the server ? and is this approach is the most suitable approach for p2p appications (Hole punching is not an option in my situation)?

This is my connections after mapping the ports and running the server ( with 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  *:*

来源:https://stackoverflow.com/questions/37237337/c-sharp-upnp-not-seems-to-be-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!