c# 使用 stunserver 完成远程计算机发送和接收数据
Posted
技术标签:
【中文标题】c# 使用 stunserver 完成远程计算机发送和接收数据【英文标题】:c# being done with a remote computer sending and receiving data using stunserver 【发布时间】:2012-05-16 07:24:10 【问题描述】:我无法使用这两个远程计算机发送数据 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();
【问题讨论】:
我使用 lumisoft 库作为 stunserver 链接= lumisoft.ee/lswww/download/downloads/Net/LumiSoft.Net.zip 【参考方案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;
【讨论】:
【参考方案2】:我遇到了同样的问题...我解决了禁用 Windows 防火墙的问题。它阻塞了所有的交通。
【讨论】:
以上是关于c# 使用 stunserver 完成远程计算机发送和接收数据的主要内容,如果未能解决你的问题,请参考以下文章