/******* http客戶端程式 httpclient.c ************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
開始///
/********************************************
功能:搜尋字串右邊起的第乙個匹配字元
********************************************/
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 httpclient.c 開始 功能 搜尋字串右邊起的第乙個匹...
Linux下http協議實現 C
include include include string h include include socket h include errno h include include include include include include ctype h int main int argc,ch...