我是WebRTC Android的新手,通过在下面的代码中使用jStun,我可以获得Android设备的公共IP和端口。
new Thread() {
@Override
public void run() {
super.run();
try {
MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
ChangeRequest changeRequest = new ChangeRequest();
sendMH.addMessageAttribute(changeRequest);
byte[] data = sendMH.getBytes();
s = new DatagramSocket(4500);
s.setReuseAddress(true);
DatagramPacket p = new DatagramPacket(data, data.length, InetAddress.getByName("stun.l.google.com"), 19302);
s.send(p);
DatagramPacket rp;
rp = new DatagramPacket(new byte[32], 32);
//Recieve Packet from Stun Server
s.receive(rp);
MessageHeader receiveMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingResponse);
receiveMH.parseAttributes(rp.getData());
final MappedAddress ma = (MappedAddress) receiveMH
.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
//Show Public IP and Port On Text View
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView myIp = findViewById(R.id.listeningIp);
TextView myPort = findViewById(R.id.listeningPort);
myIp.setText(ma.getAddress().toString());
myPort.setText(ma.getPort() + "");
}
});
while (true) {
byte[] b = new byte[1024];
DatagramPacket rr = new DatagramPacket(b, 0, b.length);
//This Recieve Aimed to Recieve from Laptop Packet Sender
s.receive(rr);
final String sss = new String(rr.getData());
Log.e("recieved", sss);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
Android设备的公共IP和端口:39.41.148.77:4500
但是,当我从笔记本电脑的数据包发送器软件向Android发送数据包时(39.41.148.77:4400)却没有收到。
感谢任何向导。谢谢
答案 0 :(得分:0)
完成已完成的STUN绑定请求并不会突然使您的端口从刚交换数据包的公共STUN服务器以外的任何其他端口接收流量。为了完全打开端口,两个端点都需要从同一本地端口向彼此已知的ip:port对发送数据包(多次,一次)。
您需要了解的所有信息都在这里: