实例介绍
【实例简介】基于sip协议的语音直播app源码
【实例截图】【核心代码】
package com.jstun.demo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import android.util.Log;
import com.jstun.core.attribute.ChangeRequest;
import com.jstun.core.attribute.ChangedAddress;
import com.jstun.core.attribute.ErrorCode;
import com.jstun.core.attribute.MappedAddress;
import com.jstun.core.attribute.MessageAttribute;
import com.jstun.core.attribute.MessageAttributeException;
import com.jstun.core.attribute.MessageAttributeParsingException;
import com.jstun.core.header.MessageHeader;
import com.jstun.core.header.MessageHeaderParsingException;
import com.jstun.core.util.UtilityException;
public class DiscoveryTest {
private static final String TAG = "DiscoveryTest";
InetAddress iaddress;
String stunServer;
int port;
int timeoutInitValue = 300; //ms
MappedAddress ma = null;
ChangedAddress ca = null;
boolean nodeNatted = true;
DatagramSocket socketTest1 = null;
public DiscoveryInfo di = null;
public DiscoveryTest(InetAddress iaddress , String stunServer, int port) {
super();
this.iaddress = iaddress;
this.stunServer = stunServer;
this.port = port;
}
public DiscoveryInfo test() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageAttributeException, MessageHeaderParsingException{
ma = null;
ca = null;
nodeNatted = true;
socketTest1 = null;
di = new DiscoveryInfo(iaddress);
test1();
// if (test1()) {
// if (test2()) {
// if (test1Redo()) {
// test3();
// }
// }
// }
socketTest1.close();
return di;
}
private boolean test1() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageHeaderParsingException {
int timeSinceFirstTransmission = 0;
int timeout = timeoutInitValue;
while (true) {
try {
// Test 1 including response
socketTest1 = new DatagramSocket(/*new InetSocketAddress(iaddress, 0)*/);
socketTest1.setReuseAddress(true);
socketTest1.connect(InetAddress.getByName(stunServer), port);
socketTest1.setSoTimeout(timeout);
MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
sendMH.generateTransactionID();
ChangeRequest changeRequest = new ChangeRequest();
sendMH.addMessageAttribute(changeRequest);
byte[] data = sendMH.getBytes();
DatagramPacket send = new DatagramPacket(data, data.length);
socketTest1.send(send);
Log.d(TAG, "Test 1: Binding Request sent.");
MessageHeader receiveMH = new MessageHeader();
while (!(receiveMH.equalTransactionID(sendMH))) {
DatagramPacket receive = new DatagramPacket(new byte[200], 200);
socketTest1.receive(receive);
receiveMH = MessageHeader.parseHeader(receive.getData());
receiveMH.parseAttributes(receive.getData());
}
ma = (MappedAddress) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
ca = (ChangedAddress) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ChangedAddress);
ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
if (ec != null) {
di.setError(ec.getResponseCode(), ec.getReason());
Log.d(TAG, "Message header contains an Errorcode message attribute.");
return false;
}
if ((ma == null) || (ca == null)) {
di.setError(700, "The server is sending an incomplete response (Mapped Address and Changed Address message attributes are missing). The client should not retry.");
Log.d(TAG, "Response does not contain a Mapped Address or Changed Address message attribute.");
return false;
} else {
di.setPublicIP(ma.getAddress().getInetAddress());
if ((ma.getPort() == socketTest1.getLocalPort()) && (ma.getAddress().getInetAddress().equals(socketTest1.getLocalAddress()))) {
Log.d(TAG, "Node is not natted.");
nodeNatted = false;
} else {
Log.d(TAG, "Node is natted.");
}
return true;
}
} catch (SocketTimeoutException ste) {
if (timeSinceFirstTransmission < 3000) { // was 7900
Log.d(TAG, "Test 1: Socket timeout while receiving the response.");
timeSinceFirstTransmission = timeout;
int timeoutAddValue = (timeSinceFirstTransmission * 2);
if (timeoutAddValue > 1600) timeoutAddValue = 1600;
timeout = timeoutAddValue;
} else {
// node is not capable of udp communication
Log.d(TAG, "Test 1: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
di.setBlockedUDP();
Log.d(TAG, "Node is not capable of UDP communication.");
return false;
}
}
}
}
private boolean test2() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageAttributeException, MessageHeaderParsingException {
int timeSinceFirstTransmission = 0;
int timeout = timeoutInitValue;
while (true) {
try {
// Test 2 including response
DatagramSocket sendSocket = new DatagramSocket(/*new InetSocketAddress(iaddress, 0)*/);
sendSocket.connect(InetAddress.getByName(stunServer), port);
sendSocket.setSoTimeout(timeout);
MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
sendMH.generateTransactionID();
ChangeRequest changeRequest = new ChangeRequest();
changeRequest.setChangeIP();
changeRequest.setChangePort();
sendMH.addMessageAttribute(changeRequest);
byte[] data = sendMH.getBytes();
DatagramPacket send = new DatagramPacket(data, data.length);
sendSocket.send(send);
Log.d(TAG, "Test 2: Binding Request sent.");
int localPort = sendSocket.getLocalPort();
InetAddress localAddress = sendSocket.getLocalAddress();
sendSocket.close();
DatagramSocket receiveSocket = new DatagramSocket(localPort, localAddress);
receiveSocket.connect(ca.getAddress().getInetAddress(), ca.getPort());
receiveSocket.setSoTimeout(timeout);
MessageHeader receiveMH = new MessageHeader();
while(!(receiveMH.equalTransactionID(sendMH))) {
DatagramPacket receive = new DatagramPacket(new byte[200], 200);
receiveSocket.receive(receive);
receiveMH = MessageHeader.parseHeader(receive.getData());
receiveMH.parseAttributes(receive.getData());
}
ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
if (ec != null) {
di.setError(ec.getResponseCode(), ec.getReason());
Log.d(TAG, "Message header contains an Errorcode message attribute.");
return false;
}
if (!nodeNatted) {
di.setOpenAccess();
Log.d(TAG, "Node has open access to the Internet (or, at least the node is behind a full-cone NAT without translation).");
} else {
di.setFullCone();
Log.d(TAG, "Node is behind a full-cone NAT.");
}
return false;
} catch (SocketTimeoutException ste) {
if (timeSinceFirstTransmission < 7900) {
Log.d(TAG, "Test 2: Socket timeout while receiving the response.");
timeSinceFirstTransmission = timeout;
int timeoutAddValue = (timeSinceFirstTransmission * 2);
if (timeoutAddValue > 1600) timeoutAddValue = 1600;
timeout = timeoutAddValue;
} else {
Log.d(TAG, "Test 2: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
if (!nodeNatted) {
di.setSymmetricUDPFirewall();
Log.d(TAG, "Node is behind a symmetric UDP firewall.");
return false;
} else {
// not is natted
// redo test 1 with address and port as offered in the changed-address message attribute
return true;
}
}
}
}
}
private boolean test1Redo() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageHeaderParsingException{
int timeSinceFirstTransmission = 0;
int timeout = timeoutInitValue;
while (true) {
// redo test 1 with address and port as offered in the changed-address message attribute
try {
// Test 1 with changed port and address values
socketTest1.connect(ca.getAddress().getInetAddress(), ca.getPort());
socketTest1.setSoTimeout(timeout);
MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
sendMH.generateTransactionID();
ChangeRequest changeRequest = new ChangeRequest();
sendMH.addMessageAttribute(changeRequest);
byte[] data = sendMH.getBytes();
DatagramPacket send = new DatagramPacket(data, data.length);
socketTest1.send(send);
Log.d(TAG, "Test 1 redo with changed address: Binding Request sent.");
MessageHeader receiveMH = new MessageHeader();
while (!(receiveMH.equalTransactionID(sendMH))) {
DatagramPacket receive = new DatagramPacket(new byte[200], 200);
socketTest1.receive(receive);
receiveMH = MessageHeader.parseHeader(receive.getData());
receiveMH.parseAttributes(receive.getData());
}
MappedAddress ma2 = (MappedAddress) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
if (ec != null) {
di.setError(ec.getResponseCode(), ec.getReason());
Log.d(TAG, "Message header contains an Errorcode message attribute.");
return false;
}
if (ma2 == null) {
di.setError(700, "The server is sending an incomplete response (Mapped Address message attribute is missing). The client should not retry.");
Log.d(TAG, "Response does not contain a Mapped Address message attribute.");
return false;
} else {
if ((ma.getPort() != ma2.getPort()) || (!(ma.getAddress().getInetAddress().equals(ma2.getAddress().getInetAddress())))) {
di.setSymmetric();
Log.d(TAG, "Node is behind a symmetric NAT.");
return false;
}
}
return true;
} catch (SocketTimeoutException ste2) {
if (timeSinceFirstTransmission < 7900) {
Log.d(TAG, "Test 1 redo with changed address: Socket timeout while receiving the response.");
timeSinceFirstTransmission = timeout;
int timeoutAddValue = (timeSinceFirstTransmission * 2);
if (timeoutAddValue > 1600) timeoutAddValue = 1600;
timeout = timeoutAddValue;
} else {
Log.d(TAG, "Test 1 redo with changed address: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
return false;
}
}
}
}
private void test3() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageAttributeException, MessageHeaderParsingException {
int timeSinceFirstTransmission = 0;
int timeout = timeoutInitValue;
while (true) {
try {
// Test 3 including response
DatagramSocket sendSocket = new DatagramSocket(/*new InetSocketAddress(iaddress, 0)*/);
sendSocket.connect(InetAddress.getByName(stunServer), port);
sendSocket.setSoTimeout(timeout);
MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
sendMH.generateTransactionID();
ChangeRequest changeRequest = new ChangeRequest();
changeRequest.setChangePort();
sendMH.addMessageAttribute(changeRequest);
byte[] data = sendMH.getBytes();
DatagramPacket send = new DatagramPacket(data, data.length);
sendSocket.send(send);
Log.d(TAG, "Test 3: Binding Request sent.");
int localPort = sendSocket.getLocalPort();
InetAddress localAddress = sendSocket.getLocalAddress();
sendSocket.close();
DatagramSocket receiveSocket = new DatagramSocket(localPort, localAddress);
receiveSocket.connect(InetAddress.getByName(stunServer), ca.getPort());
receiveSocket.setSoTimeout(timeout);
MessageHeader receiveMH = new MessageHeader();
while (!(receiveMH.equalTransactionID(sendMH))) {
DatagramPacket receive = new DatagramPacket(new byte[200], 200);
receiveSocket.receive(receive);
receiveMH = MessageHeader.parseHeader(receive.getData());
receiveMH.parseAttributes(receive.getData());
}
ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
if (ec != null) {
di.setError(ec.getResponseCode(), ec.getReason());
Log.d(TAG, "Message header contains an Errorcode message attribute.");
return;
}
if (nodeNatted) {
di.setRestrictedCone();
Log.d(TAG, "Node is behind a restricted NAT.");
return;
}
} catch (SocketTimeoutException ste) {
if (timeSinceFirstTransmission < 7900) {
Log.d(TAG, "Test 3: Socket timeout while receiving the response.");
timeSinceFirstTransmission = timeout;
int timeoutAddValue = (timeSinceFirstTransmission * 2);
if (timeoutAddValue > 1600) timeoutAddValue = 1600;
timeout = timeoutAddValue;
} else {
Log.d(TAG, "Test 3: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
di.setPortRestrictedCone();
Log.d(TAG, "Node is behind a port restricted NAT.");
return;
}
}
}
}
}
标签: IP
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论