http協議C語言程式設計例項

2021-05-21 12:52:30 字數 2692 閱讀 7152

/******* http客戶端程式 httpclient.c ************/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

//httpclient.c 開始///

/********************************************

功能:搜尋字串右邊起的第乙個匹配字元

********************************************/

char * rstrchr(char * s, char x)  

/********************************************

功能:把字串轉換為全小寫

********************************************/

void tolowercase(char * s)  

if(argc!=2)

printf("parameter.1 is: %s/n", argv[1]);

tolowercase(argv[1]);/*將引數轉換為全小寫*/

printf("lowercase parameter.1 is: %s/n", argv[1]);

gethost(argv[1], host_addr, host_file, &portnumber);/*分析**、埠、檔名等*/

printf("webhost:%s/n", host_addr);

printf("hostfile:%s/n", host_file);

printf("portnumber:%d/n/n", portnumber);

if((host=gethostbyname(host_addr))==null)/*取得主機ip位址*/

/* 客戶程式開始建立 sockfd描述符 */

if((sockfd=socket(af_inet,sock_stream,0))==-1)/*建立socket連線*/

/* 客戶程式填充服務端的資料 */

bzero(&server_addr,sizeof(server_addr));

server_addr.sin_family=af_inet;

server_addr.sin_port=htons(portnumber);

server_addr.sin_addr=*((struct in_addr *)host->h_addr);

/* 客戶程式發起連線請求 */

if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)/*連線***/

printf("%s", request);/*準備request,將要傳送給主機*/

/*取得真實的檔名*/

if(host_file && *host_file)   pt = rstrchr(host_file, '/');

else pt = 0;

memset(local_file, 0, sizeof(local_file));

if(pt && *pt)  

else if(host_file && *host_file)   strcpy(local_file, host_file);

else   strcpy(local_file, "index.html");

printf("local filename to write:%s/n/n", local_file);

/*傳送http請求request*/

send = 0;totalsend = 0;

nbytes=strlen(request);

while(totalsend < nbytes)

totalsend+=send;

printf("%d bytes send ok!/n", totalsend);

}fp = fopen(local_file, "a");

if(!fp)  

printf("/nthe following is the response header:/n");

i=0;

/* 連線成功了,接收http響應,response */

while((nbytes=read(sockfd,buffer,1))==1)

else  

}fclose(fp);

/* 結束通訊 */

close(sockfd);

exit(0);

}

HTTP協議的C語言程式設計實現例項

客戶程式發起連線請求 if connect sockfd,struct sockaddr server addr sizeof struct sockaddr 1 連線 printf s request 準備request,將要傳送給主機 取得真實的檔名 if host file host file...

HTTP協議的C語言程式設計實現例項

http客戶端程式 httpclient.c include include include include include include include include include include include include 開始 功能 搜尋字串右邊起的第乙個匹配字元 char rstr...

C語言程式設計例項

c語言三個數從小到大排序 輸出 c語言猴子吃桃問題 c語言百錢買百雞 百錢百雞,百雞問題 c語言漁夫打魚曬網問題 c語言希爾排序演算法 c語言氣泡排序演算法 c語言直接插入排序演算法 c語言快速排序演算法 c語言選擇排序演算法 c語言歸併排序演算法 c語言二分查詢演算法,折半查詢演算法 c語言分塊查...