中级会员
 
- 积分
- 260
- 金钱
- 260
- 注册时间
- 2013-1-10
- 在线时间
- 10 小时
|
5金钱
原子哥及各位神 ,搜过论坛了 学习代码时遇到了一个问题了
//处理HTTP输入数据
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())这样的语句?
谢谢
|
最佳答案
查看完整内容[请看2#楼]
为什么不会呢?
这就是指针操作,指针指向数组,然后操作指针所指向地址的数据,实际上就是修改数组内容。
好好学习下指针的使用。
|