首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

网上一个上传文件到http协议服务器//二星参数使用有关问题

2013-09-08 
网上一个上传文件到http协议服务器//二星参数使用问题memset(*response,0,len) //二星的参数,就三个地方

网上一个上传文件到http协议服务器//二星参数使用问题
memset(*response,0,len); //二星的参数,就三个地方使用了 我用个一星不也能拷贝地址内容吗?
memcpy(*response,ptmp,len-1);
if(atoi(h_post)<len)
(*response)[atoi(h_post)] = '\0';


int http_upload(http_tcpclient *pclient,char *page, char *filename, char **response)
{
intfd;
char*lpbuf, *ptmp, *fbuf;
intlen, nSize;
charh_post[128], h_host[128], h_content_len[128], h_content_type[256], h_content_str[1024];

const char *h_header="User-Agent: Mozilla/4.0\r\nCache-Control: no-cache\r\nAccept: */*\r\nAccept-Language: zh-cn\r\nConnection: Keep-Alive\r\n";
const char *h_data_boun="---------------------------7d9ab1c50098";

//开始组包;
memset(h_post, 0, sizeof(h_post));
sprintf(h_post, "POST %s HTTP/1.1\r\n", page);
memset(h_host, 0, sizeof(h_host));
sprintf(h_host, "HOST: %s:%d\r\n",pclient->remote_ip, pclient->remote_port);
memset(h_content_type, 0, sizeof(h_content_type));
sprintf(h_content_type, "Content-Type: multipart/form-data; boundary=%s\r\n", h_data_boun);

memset(h_content_str, 0, sizeof(h_content_str));
snprintf(h_content_str, 1023, "--%s\r\nContent-Disposition: form-data; name="fileName"; filename="%s"\r\n\r\n", h_data_boun, filename);
//开始读取文件内容
if (filename == NULL || (fd = open(filename, O_RDONLY)) == -1) {
return -6;
}
nSize = lseek(fd, 0, SEEK_END);
if (nSize == -1) return -2;
lseek(fd, (off_t)0, SEEK_SET);
fbuf = (char *)calloc(nSize + 1, sizeof(char));
if (NULL == fbuf){
return -7;
}
if (read(fd, fbuf, nSize) == -1) return -8;
fbuf[nSize] = 0;

memset(h_content_len, 0, sizeof(h_content_len));
sprintf(h_content_len,"Content-Length: %d\r\n\r\n", strlen(h_content_str)+nSize+strlen(h_data_boun)+6);
len = strlen(h_post)+strlen(h_host)+strlen(h_header)+strlen(h_content_len)+strlen(h_content_type)+nSize+strlen(h_data_boun)+6+1;
lpbuf = (char*)malloc(len);
if(lpbuf==NULL){
printf("Malloc error.\n");
return -1;
}
strcpy(lpbuf,h_post);
strcat(lpbuf,h_host);
strcat(lpbuf,h_header);
strcat(lpbuf,h_content_type);
strcat(lpbuf,h_content_len);
strcat(lpbuf,"\r\n");
strcat(lpbuf,h_content_str);
strcat(lpbuf,fbuf);
strcat(lpbuf,"\r\n");
memset(h_content_str, 0, sizeof(h_content_str));
sprintf(h_content_str, "--%s--", h_data_boun);


strcat(lpbuf,h_content_str);
strcat(lpbuf,"\r\n");

//发送包
if(http_tcpclient_send(pclient,lpbuf,len)<0){
free(lpbuf);
return -2;
}
free(lpbuf);

//接收包
if(http_tcpclient_recv(pclient,&lpbuf,0) <= 0){
return -3;
}

/*响应代码,|HTTP/1.1 200 OK|
 *从第10个字符开始,共3位
 * */
memset(h_post,0,sizeof(h_post));
strncpy(h_post,lpbuf+9,3);
if(atoi(h_post)!=200){
if(lpbuf) free(lpbuf);
return atoi(h_post);
}
ptmp = (char*)strstr(lpbuf,"\r\n\r\n");
if(ptmp == NULL){
free(lpbuf);
return -4;
}
ptmp += 4;/*跳过\r\n*/

len = strlen(ptmp)+1;
*response=(char*)malloc(len);
if(*response == NULL){
if(lpbuf) free(lpbuf);
return -5;
}
memset(*response,0,len);
memcpy(*response,ptmp,len-1);

/*从头域找到内容长度,如果没有找到则不处理*/
ptmp = (char*)strstr(lpbuf,"Content-Length:");
if(ptmp != NULL){
char *ptmp2;
ptmp += 15;
ptmp2 = (char*)strstr(ptmp,"\r\n");
if(ptmp2 != NULL){
memset(h_post,0,sizeof(h_post));
strncpy(h_post,ptmp,ptmp2-ptmp);
if(atoi(h_post)<len)
(*response)[atoi(h_post)] = '\0';
}
}

if(lpbuf) free(lpbuf);
return 0;
}


[解决办法]
两星。。。一星,你这叫法还真白。
那是指针的指针,是为了在函数体内给你申请内存,并且能将地址传出用的。
[解决办法]
指针的指针吧,
如果外层定义的一个空指针了,因为不知道大小,
所以传双指针进来,需要在接口里动态分配内存空间,来保存服务端回应的消息buf

热点排行