C# 正在通过远程计算机使用 StunServer 发送和接收数据

本文关键字:StunServer 数据 计算机 | 更新日期: 2023-09-27 18:33:30

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

我正在使用我的程序,尤物

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

C# 正在通过远程计算机使用 StunServer 发送和接收数据

我正在处理同样的问题,并使用相同的库。

您是否检查了是否获得了端点?我发现我们的指向服务器不在线。所以我做了一个服务器列表。

我获得公开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;
        }

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