从url中截取ip

使用的url
rtsp://admin:dowse123@192.168.1.31:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1

rtsp://192.168.1.31:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1

rtsp://192.168.1.31/Streaming/Channels/101?transportmode=unicast&profile=Profile_1

特点:
1 @...../
2 //......:
3 //......./
代码记录如下

#ifndef DPRINTK
#define DPRINTK(fmt, args...)	printf("(%s,%d)%s: " fmt,__FILE__,__LINE__, __FUNCTION__ , ## args)
#endif


static int FindSTR( char  * byhtml, char *cfind, int nStartPos)
{
	int i;
	long length;
	char * tmphtml;
	char * temfind ;

	
	if( byhtml == NULL || cfind == NULL)
		return -1;

	tmphtml =(char *) byhtml + nStartPos;

	temfind = cfind;

	if(nStartPos < 0)
		return -1;

	length = strlen(tmphtml); 
	
	for( i = 0 ; i < length; i++)
	{		

		if( *tmphtml != *temfind )
		{
			tmphtml++;

			continue;
		}

		while( *tmphtml == *temfind )
		{
			//printf(" %c.%c|",*tmphtml,*temfind);
			tmphtml++;
			temfind++;

			if( *temfind == (char)NULL ) // 找到了。
			{			
				return nStartPos + i;
			}
		}

		//printf("\n");	
		
		if( *temfind == (char)NULL ) // 找到了。
		{			
			return nStartPos + i;
		}
		else
		{	// 从与temfind首字节相同的那一位的后一位重新开始找,
			temfind = cfind;
			tmphtml = (char *)byhtml + nStartPos + i + 1;
		}
	}

	return -1;
}



// start 是指 '<' 所在的位置,end 指 '>'的位置,此函数取两个位置之间的那部分字符串。
static int GetTmpLink(char * byhtml, char * temp, int start, int end,int max_num)
{
	int i;
	
	if(  byhtml == NULL ||  temp == NULL)
		return -1;

	if( end - start > max_num )
	{		
		temp[0] = (char)NULL;
		return -1;
	}	

	for(i = start + 1; i < end ; i++)
	{
		*temp = byhtml[i];

		temp++;
	}

	*temp = (char)NULL; // 结束符。

	return 1;
}





static int getUrlIp(char * ip,const char * url)
{
	char tmp_ip[100] ={0};
	char tmp_user[100] = {0};
	char tmp_pass[100] = {0};
	char tmp_url[100] = {0};
	char tmp_url2[100] = {0};
	int len = 0;
	int i = 0;
	int j = 0;
	int rel;	

	strcpy(tmp_url,url);
	
	DPRINTK("============ FindSTR  url:%s\n",url);
	j = FindSTR(tmp_url,"@",0);
	DPRINTK("=========== FindSTR \n");
	if( j >=  0 )
	{
		//url中有用户信息,要提取出来。
		rel = GetTmpLink(tmp_url,tmp_user,strlen("rtsp://")-1,j,30);
		if( rel < 0)
		{
			DPRINTK("Can't get user info: %s\n",url);
			return -1;
		}else
		{
			DPRINTK("get tmp %s\n",tmp_user);
			for( i = 0; i < strlen(tmp_user); i++)
			{
				if( tmp_user[i] == ':')
				{
					strcpy(tmp_pass, &tmp_user[i+1]);
					tmp_user[i] = 0;
					DPRINTK("get user %s  %s\n",tmp_user,tmp_pass);
					break;
				}
			}
		}
		
		sprintf(tmp_url2,"rtsp://%s",&tmp_url[j+1]);
		DPRINTK("get url2 %s\n",tmp_url2);
		strcpy(tmp_url,tmp_url2);
	}	
	
	sscanf(tmp_url,"rtsp://%s",tmp_ip);	

	DPRINTK("==============tmp_ip:%s\n",tmp_ip);

	len = strlen(tmp_ip);
	for( i = 0; i < len ; i++)
	{
		if( tmp_ip[i] == ':' )
			tmp_ip[i] = 0;

		if( tmp_ip[i] == '/' )
			tmp_ip[i] = 0;
	}
	

	if( tmp_ip[0] != 0 )
		strcpy(ip,tmp_ip);
	else
		return -1;

	return 1;
}


//校验url地址是否通畅
static bool checkUrl(const QString &url, int checkTime)
{
    //找出IP地址
    QRegExp reg("((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))");
    reg.indexIn(url);
    QString ip = url.mid(url.indexOf(reg), reg.matchedLength());

    //找出端口号
    int port = 554;
    QStringList list = url.split(":");
    if (list.count() > 2) {
        int index = 2;
        if (url.contains("@")) {
            index = 3;
        }

        list = list.at(index).split("/");
        QString str = list.first();
        if (!str.isEmpty()) {
            port = str.toInt();
        }
    }

    //局部的事件循环,不卡主界面
    QEventLoop eventLoop;

    //设置超时
    QTimer timer;
    QObject::connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
    timer.setSingleShot(true);
    timer.start(checkTime);

    QTcpSocket tcpSocket;
    QObject::connect(&tcpSocket, SIGNAL(connected()), &eventLoop, SLOT(quit()));
    tcpSocket.connectToHost(ip, port);
    eventLoop.exec();

    //超时没有连接上则判断该摄像机不在线
    if (tcpSocket.state() != QAbstractSocket::ConnectedState) {
        qDebug() << TIMEMS << url << "connect error";
        return false;
    }

    return true;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值