1

我无法使用这两个远程计算机发送数据 stunserver。数据来自本地计算机,但远程计算机上的数据不会到来。

我正在使用我的程序 stunserver

public void run() 
{
    UpdateText("Now Listening..");
    remoteSender = new IPEndPoint(IPAddress.Any, 0);
    tempRemoteEP = (EndPoint)remoteSender;
    byte[] packet = new byte[1024];
    while (true)
    {
        if (socket.Available > 0)
        {
            this.nbBytesRx = socket.ReceiveFrom(packet, ref tempRemoteEP);
            nbPackets++;
            seqNo = BitConverter.ToInt16(packet, 0);
            UpdateText(nbPackets.ToString() + ":" + seqNo.ToString() + " / ");
        }
    }
}

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,  ProtocolType.Udp);

socket.Bind(new IPEndPoint(IPAddress.Any, 0));
string localEP = socket.LocalEndPoint.ToString();
string publicEP = "";
string netType = "";
STUN_Result result = STUN_Client.Query("stunserver.org", 3478, socket);
netType = result.NetType.ToString();
if (result.NetType != STUN_NetType.UdpBlocked)
{
    publicEP = result.PublicEndPoint.ToString();
}
else
{
    publicEP = "";
}
UpdateText("Local EP:" + localEP);
UpdateText("Public EP:" + publicEP);

ThreadStart startMethod = new ThreadStart(this.run);
thread = new Thread(startMethod);
thread.Start();
4

2 回答 2

1

我正在解决同样的问题,并使用相同的库。

你检查你是否得到你的端点?我发现我们那个指向的服务器不在线。所以我做了一个服务器列表来通过。

我获得公共 EP 的方法:

public static IPEndPoint GetMyPublicEP() {

// Create new socket for STUN client.
            Socket _socket = new Socket
                (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            _socket.Bind(new IPEndPoint(IPAddress.Any, 0));

            List<string> _stunServers = new List<string> { // список стан серверов для проверки своей ендпоинт, не все работают
                "stun.l.google.com:19302",
                "stun1.l.google.com:19302",
                "stun2.l.google.com:19302",
                "stun3.l.google.com:19302",
                "stun4.l.google.com:19302",
                "stun01.sipphone.com",
                "stun.ekiga.net",
                "stun.fwdnet.net",
                "stun.ideasip.com",
                "stun.iptel.org",
                "stun.rixtelecom.se",
                "stun.schlund.de",
                "stunserver.org",
                "stun.softjoys.com",
                "stun.voiparound.com",
                "stun.voipbuster.com",
                "stun.voipstunt.com",
                "stun.voxgratia.org",
                "stun.xten.com"
            };

            foreach (string server in _stunServers)
            {
                try
                {
                    STUN_Result result = STUN_Client.Query(server, 3478, _socket);
                    IPEndPoint myPublicEPStun = result.PublicEndPoint;

                    if (myPublicEPStun != null)
                    {
                        _myPublicEPStun = myPublicEPStun;
                        break;
                    }
                }
                catch (Exception E)
                {
                    Console.WriteLine("Якась необ`ясніма магія трапилась");
                }
            }
            return _myPublicEPStun;
        }
于 2017-04-10T22:26:03.910 回答
0

我有同样的问题......我解决了它禁用 Windows 防火墙。它阻塞了所有的交通。

于 2017-09-23T10:56:44.440 回答