对于数据的粘包处理

感谢我师父的指导  http://my.csdn.net/innovation_miracle

#include <stdio.h>
#include <regex.h>
#include <stdlib.h>
#include <string.h>

#define ERRBUF 128
char protmatch(char *buf, regmatch_t match[])
{
	regex_t myreg;
	 char err[ERRBUF];
	int resulta,resultb;
	//char pattern[] = "\\*HH,[\\w,]*#";
	char pattern[] = "\\*HH,[0-9,A-Z.a-z]*#";
	//char pattern[]="[0-9]{3}";
	resulta = regcomp(&myreg, pattern, REG_EXTENDED | REG_NEWLINE);
	if(resulta != 0)
  {
  	printf("3333333\n");
    
    regerror(resulta, &myreg, err, sizeof(err));
    regfree(&myreg);
    return 0;
  }
  resultb = regexec(&myreg, buf, 2, match, 0);
  if(resultb != 0)
  {
  //		printf("22222\n");
          regerror(resultb, &myreg, err, sizeof(err));
          regfree(&myreg);
          return 0;
  }
  
  regfree(&myreg);
	return 1;
}

static char * substr(const char * str, unsigned start, unsigned end)
{
        unsigned n = end - start;
        static char strbuf[256];
        memset(strbuf,'\0',sizeof(strbuf));
        strncpy(strbuf, str + start ,n);
        strbuf[n] = '\0';
        return strbuf;
}

int main()
{
	regmatch_t match[2];
	char *p=NULL;
	p="*HH,111111,222222,FFFFFF#*HH,33333,4444444,FFFFFF#*HH,555555,666666,SSSSSS#";
	//printf("p= %s \n",p);
      FILE *fd;	
	char gets_buf[1024];
	//fd = fopen("./lys.txt","r");
    
   // while(fgets(gets_buf,1024,fd)!=0)
//	{
		//p = gets_buf;
	while(protmatch(p,match)==1)
	{
		if(match[0].rm_so != -1)
		{
			printf("pppp=%s \n ",substr(p, match[0].rm_so, match[0].rm_eo));
			p=&p[match[0].rm_eo];
		}
		else 
		{
			break;
		}
	}
//	}
	return 0;
}


发布了55 篇原创文章 · 获赞 28 · 访问量 9万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览