新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 學習uip代碼分析時遇到的c語言問題

學習uip代碼分析時遇到的c語言問題

作者: 時間:2016-11-27 來源:網(wǎng)絡 收藏
在進一步看uip的代碼時,遇到了一個問題,可能是自己C語言知識不夠扎實,特此總結(jié)一下

以下是http協(xié)議處理輸入數(shù)據(jù)并更新頁面顯示的代碼

本文引用地址:http://butianyuan.cn/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地址之后)數(shù)據(jù),是否是"/"
if(s->inputbuf[1] == ISO_space||s->inputbuf[1] == ?) //第二個數(shù)據(jù)是空格/問號
{
if(s->inputbuf[1]==?&&s->inputbuf[6]==0x31)//LED1
{
LED0=!LED0;
strx=strstr((const char*)(data_index_html+13),"LED0狀態(tài)");
if(strx)//存在"LED0狀態(tài)"這個字符串
{
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狀態(tài)");
if(strx)//存在"LED1狀態(tài)"這個字符串
{
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);
}
這部分代碼中,是處理輸入數(shù)據(jù)的,比如接收到的數(shù)據(jù)是把LED0滅掉,會更新網(wǎng)頁顯示的圖片使其變成灰色,并顯示“滅”字,同時更新時間和溫度。那我想問了strx=strstr((const char*)(data_index_html+13),"℃");//在數(shù)組里找到"℃"字符后,又調(diào)用strncpy(strx-4,(const char*)dbuf,4); 我知道是把dbuf里的數(shù)據(jù)copy到strx-4的地址里邊去了,但怎么會更新到data_index_html[]里邊去的呢?這樣是怎么實現(xiàn)網(wǎng)頁顯示更新呢?后來也沒有出現(xiàn)strx?。渴遣皇菓撚衧trcpy(data_index_html,str,sizeof())這樣的語句?

實際上,這是c語言的指針地址的關系。

int *p;//定義一個指針P

int a=10;//定義一個變量a賦初值10

p=&a;//&是求地址運算符 將a的地址賦給p p指向a的首地址,如果p指向數(shù)組 那么p就是數(shù)組的首地址strx=&data_index_html;

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

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

P是指針,那么*p就是內(nèi)容。

下面看vc打印

int main()
{

char *strx;

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

strx=strstr((const char*)(data_index_html+2),"a"); //尋找a出現(xiàn)在data_index_html的位置 并返回其首地址
printf("%s",strx);//打印data_index_html的首地址 并以流形式 輸出知道遇到