基于http协议的程序.docxVIP

  • 6
  • 0
  • 约4.42千字
  • 约 6页
  • 2017-08-16 发布于重庆
  • 举报
基于http协议的程序

本软件适用于unix/linux平台,是一款基于http协议的文件下载程序。分为两个文件第一个文件是:http_download.h第二个文件:http_download.c下面是关于http_download.h的源代码:#ifndefhttp_download__h#define http_download__h#endif#includestdio.h#includeunistd.h#includestdlib.h#includestring.h#includesys/socket.h#includesys/types.h#includenetinet/in.h#includearpa/inet.h#includectype.h#includeerrno.hintparase_len(char* buf);voidGetHost(char* url,char* ip_port,char* file);voidGet_IP_PORT(char* ip_port,char* ip,char* port);intDownLoad (char* url,char* ret_buf);把上面的代码复制到http_download.h中即可。下面是http_download.cpp的源代码,复制后粘贴到http_download.c中即可。#include http_download.h/** *filename: http_download.c *purpose: HTTP协议客户端,可以用来下载文件文本,压缩包等文件 *specifications:此程序接收的URL格式为eg::80/filename * 不支持域名解析。 * 下载速度:10KB/循环周期 *date time: 2013-12-19 *author: LiBangZhu *place :HangZhou * *//*------------------------------------------------------------------------------ *此函数主要根据参数中的url,解析出IP:PORT 和将要下载的文件名FILE,并分别存放在ip_port和 file 中 * ----------------------------------------------------------------------------*/voidGetHost(char* url,char*ip_port,char* file){char* PA;char* PB;if(url == NULL)return; PA = url;if(!strncmp(PA,http://,strlen(http://))) PA = url+strlen(http://); PB = strchr(PA,/);if(PB) {memcpy(ip_port,PA,strlen(PA)-strlen(PB));if(PB+1) {memcpy(file,PB+1,strlen(PB)-1);file[strlen(PB)-1] = 0; } }}/*------------------------------------------------------------------------------- *此函数根据传送的IP:PORT 解析出IP 和PORT 并存放在ip和 port 所指向的内存中 * * -----------------------------------------------------------------------------*/voidGet_IP_PORT(char*ip_port,char* ip,char* port){char* PB=ip_port;char* PA=strchr(ip_port,:);strncpy(port,PA+1,strlen(ip_port)-strlen(PA+1));strncpy(ip,PB,PA-PB);ip[strlen(ip)] = 0;}/*-------------------------------------------------------------------------------对外只显示此函数的调用返回正值表示下载成功,-1 表示下载不完全 -2表示连接服务器失败-3 表示创建sockfd失败 --------

文档评论(0)

1亿VIP精品文档

相关文档