新闻中心

EEPW首页>嵌入式系统>设计应用> 学习uip代码分析时遇到的c语言问题

学习uip代码分析时遇到的c语言问题

作者: 时间:2016-11-27 来源:网络 收藏
在进一步看uip的代码时,遇到了一个问题,可能是自己C语言知识不够扎实,特此总结一下

以下是http协议处理输入数据并更新页面显示的代码

本文引用地址://m.amcfsurvey.com/article/201611/322300.htm

static PT_THREAD(handle_input(struct httpd_state *s))
{
char *strx;
u8 dbuf[17];
PSOCK_BEGIN(&s->sin);
PSOCK_READTO(&s->sin, ISO_space);
if(strncmp(s->inputbuf, http_get, 4)!=0)PSOCK_CLOSE_EXIT(&s->sin); //比较客户端浏览器输入的指令是否是申请WEB指令 “GET ”
PSOCK_READTO(&s->sin, ISO_space); //" "
if(s->inputbuf[0] != ISO_slash)PSOCK_CLOSE_EXIT(&s->sin); //判断第一个(去掉IP地址之后)数据,是否是"/"
if(s->inputbuf[1] == ISO_space||s->inputbuf[1] == ?) //第二个数据是空格/问号
{
if(s->inputbuf[1]==?&&s->inputbuf[6]==0x31)//LED1
{
LED0=!LED0;
strx=strstr((const char*)(data_index_html+13),"LED0状态");
if(strx)//存在"LED0状态"这个字符串
{
strx=strstr((const char*)strx,"color:#");//找到"color:#"字符串
if(LED0)//LED0灭
{
strncpy(strx+7,"5B5B5B",6); //灰色
strncpy(strx+24,"灭",2); //灭
strx=strstr((const char*)strx,"http:");//找到"http:"字符串
strncpy(strx,(const char*)LED_OFF_PIC_ADDR,strlen((const char*)LED_OFF_PIC_ADDR));//LED0灭图片
}else
{
strncpy(strx+7,"FF0000",6); //红色
strncpy(strx+24,"亮",2); //"亮"
strx=strstr((const char*)strx,"http:");//找到"http:"字符串
strncpy(strx,(const char*)LED0_ON_PIC_ADDR,strlen((const char*)LED0_ON_PIC_ADDR));//LED0亮图片
}
}
}else if(s->inputbuf[1]==?&&s->inputbuf[6]==0x32)//LED2
{
LED1=!LED1;
strx=strstr((const char*)(data_index_html+13),"LED1状态");
if(strx)//存在"LED1状态"这个字符串
{
strx=strstr((const char*)strx,"color:#");//找到"color:#"字符串
if(LED1)//LED1灭
{
strncpy(strx+7,"5B5B5B",6); //灰色
strncpy(strx+24,"灭",2); //灭
strx=strstr((const char*)strx,"http:");//找到"http:"字符串
strncpy(strx,(const char*)LED_OFF_PIC_ADDR,strlen((const char*)LED_OFF_PIC_ADDR));//LED1灭图片
}else
{
strncpy(strx+7,"00FF00",6); //绿色
strncpy(strx+24,"亮",2); //"亮"
strx=strstr((const char*)strx,"http:");//找到"http:"字符串
strncpy(strx,(const char*)LED1_ON_PIC_ADDR,strlen((const char*)LED1_ON_PIC_ADDR));//LED1亮图片
}
}
}
strx=strstr((const char*)(data_index_html+13),"℃");//找到"℃"字符
if(strx)
{
get_temperature(dbuf); //得到温度
strncpy(strx-4,(const char*)dbuf,4); //更新温度
}
strx=strstr((const char*)strx,"RTC时间:");//找到"RTC时间:"字符
if(strx)
{
get_time(dbuf); //得到时间
strncpy(strx+33,(const char*)dbuf,16); //更新时间
}
strncpy(s->filename, http_index_html, sizeof(s->filename));
}else //如果不是 /?
{
s->inputbuf[PSOCK_DATALEN(&s->sin)-1] = 0;
strncpy(s->filename,&s->inputbuf[0],sizeof(s->filename));
}
s->state = STATE_OUTPUT;
while(1)
{
PSOCK_READTO(&s->sin, ISO_nl);
if(strncmp(s->inputbuf, http_referer, 8) == 0)
{
s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0;
}
}
PSOCK_END(&s->sin);
}
这部分代码中,是处理输入数据的,比如接收到的数据是把LED0灭掉,会更新网页显示的图片使其变成灰色,并显示“灭”字,同时更新时间和温度。那我想问了strx=strstr((const char*)(data_index_html+13),"℃");//在数组里找到"℃"字符后,又调用strncpy(strx-4,(const char*)dbuf,4); 我知道是把dbuf里的数据copy到strx-4的地址里边去了,但怎么会更新到data_index_html[]里边去的呢?这样是怎么实现网页显示更新呢?后来也没有出现strx啊?是不是应该有strcpy(data_index_html,str,sizeof())这样的语句?

实际上,这是c语言的指针地址的关系。

int *p;//定义一个指针P

int a=10;//定义一个变量a赋初值10

p=&a;//&是求地址运算符 将a的地址赋给p p指向a的首地址,如果p指向数组 那么p就是数组的首地址strx=&data_index_html;

*p=20;//那么*p就是a

printf("%s",a);打印输出20.

P是指针,那么*p就是内容。

下面看vc打印

int main()
{

char *strx;

unsigned char data_index_html[]={"1234567890abcdefg"};

strx=strstr((const char*)(data_index_html+2),"a"); //寻找a出现在data_index_html的位置 并返回其首地址
printf("%s",strx);//打印data_index_html的首地址 并以流形式 输出知道遇到结束
printf("%c",*strx);//打印内容 只打印一个首地址所在的内容
printf("%c",*strx+1);
printf("%c",*strx+2);
if(strx)
{
strncpy(strx+2,"MY",2);

}
printf("%s",data_index_html);
}

这样就不难理解,index_html_data打印的数据,因为他是通过改变地址来改变的数据。

延伸拓展

在c语言中,strstr()函数返回就是地址。



评论


技术专区

关闭