url:http://mat1.gtimg.com/www/images/qq2012/guide_k_04.png
picture_name:guide_k_04.png
sprintf(str_get,"GET / HTTP/1.1\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection:Close\r\n\r\n",url);
ret1 = send(sockfd, (void *)str_get, strlen(str_get), 0);发送成功
可是接收 i = recv(sockfd, (void *)buf, 50, 0);
却始终为0
什么情况 发送错了???
17 个解决方案
#1
没人么 给自己顶!!
#2
人呢??
#3
难不成,发送的请求有点问题
#4
你是靠主动轮循去接收吗?具体的recv是系统函数还是你自己写的,你写的socket其它通讯是OK的吗?
#5
另外,你这个SOCKET连接是连接到哪个服务器哪个端口这个要考虑好,一般情况下是要连网关的
#6
请他的都ok
第一次写有点乱
你看下
第一次写有点乱
你看下
#include "downpicture.h"
void down_picture(char * url, const char * filename){
FILE * fp;
int sockfd;
int ret;
int ret1;
int i;
int h;
struct hostent * host;
struct sockaddr_in servaddr;
char str1[4096];
char buf[BUFSIZE];
char *str;
char str_get[4096];
socklen_t len;
fd_set t_set1;
struct timeval tv;
host = gethostbyname("www.qq.com");
if(fp < 0)
{
printf("open file fail...\n");
return;
}else
printf("file create success\n");
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
printf("socket error!\n");
exit(0);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr = *((struct in_addr*)host->h_addr);
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("connect error!\n");
exit(0);
}
printf("connect success \n");
memset(str1, 0, 4096);
sprintf(str1,"GET / HTTP/1.0\r\nHost:%s\r\nContent-Type=text/plain\r\nConnection:keep-alive\r\n\r\n","www.qq.com");
ret = send(sockfd,(void *)str1,strlen(str1),0);
if (ret < 0)
{
printf("send error %d,Error message'%s'\n",errno, strerror(errno));
exit(0);
}
else
{
printf("send success ,total send %d \n", ret);
}
while(1)
{
tv.tv_sec= 0;
tv.tv_usec= 0;
h= 0;
FD_ZERO(&t_set1);
FD_SET(sockfd, &t_set1);
h= select(sockfd +1, &t_set1, NULL, NULL, &tv);
if (h == 0) continue;
if (h < 0)
{
close(sockfd);
printf("some thing read error!\n");
return;
}
if (h > 0)
{
memset(buf, 0, 4095);
i= recv(sockfd, (void *)buf, 4092,0);
if (i==0)
{
memset(str_get, 0, 4096);
sprintf(str_get,"GET / HTTP/1.1\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection:Close\r\n\r\n",url);
//sprintf(str_get,"GET %s HTTP/1.1\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection:keep-alive\r\n\r\n",filename, "http://mat1.gtimg.com");
ret1 = send(sockfd, (void *)str_get, strlen(str_get), 0);
if(ret1 < 0){
printf("resend error %d,%s", errno, strerror(errno));
exit(0);
}else
printf("resend success, total is:%d\n", ret1);
memset(buf, 0, 4095);
while(1){
i = recv(sockfd, (void *)buf, 50, 0);
if(i == 0){
close(sockfd);
printf("read picture end\n");
return;
}
else{
fp = fopen(filename,"a");
fwrite(buf, 1, i, fp);
}
}
}
}
}
fclose(fp);
close(sockfd);
}
#7
这个是连接qq.com的 解析出来的图片地址
#8
网关/》???
#9
GET 请求格式不对呀
sprintf(str,
"GET /www/images/qq2012/guide_k_04.png HTTP/1.1\r\n"
"Host: mat1.gtimg.com\r\n"
"Connection: close\r\n"
"\r\n"
);
sprintf(str,
"GET /www/images/qq2012/guide_k_04.png HTTP/1.1\r\n"
"Host: mat1.gtimg.com\r\n"
"Connection: close\r\n"
"\r\n"
);
#10
试试
#11
GET 请求格式不对呀
sprintf(str,
"GET /www/images/qq2012/guide_k_04.png HTTP/1.1\r\n"
"Host: mat1.gtimg.com\r\n"
"Connection: close\r\n"
"\r\n"
);
请求格式也改了 也确实接收到数据 可是我接收数据创建图片 却0字节
sprintf(str_get,"GET %s HTTP/1.0\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection:C lose\r\n\r\n",url_get, url_host);
90 //sprintf(str_get,"GET %s HTTP/1.1\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection: keep-alive\r\n\r\n",filename, "http://mat1.gtimg.com");
91 ret1 = send(sockfd, (void *)str_get, strlen(str_get), 0);
92 if(ret1 < 0){
93 printf("resend error %d,%s", errno, strerror(errno));
94 exit(0);
95 }else
96 printf("resend success, total is:%d\n", ret1);
97 memset(buf, 0, 4095);
98 while(1){
99 i = recv(sockfd, (void *)buf, 50, 0);
100 if(i == 0){
101 close(sockfd);
102 printf("read picture end\n");
103 return;
104 }
105 else{
106 fp = fopen(filename,"a");
107 fwrite(buf, 1, i, fp);
108 }
109 }
#12
网关/》???
另外,你这个SOCKET连接是连接到哪个服务器哪个端口这个要考虑好,一般情况下是要连网关的
#13
返回的HTTP报文你解析对了没有:
Content-Length: 13704
HTTP 头之后跟的 13704 字节就是PNG文件的内容。
HTTP/1.1 200 OK
Server: cws/3.8.3
Date: Thu, 23 Oct 2014 02:34:21 GMT
Content-Type: image/png
Content-Length: 13704
Connection: close
Last-Modified: Wed, 14 May 2014 02:03:23 GMT
ETag: "5372ceeb-3588"
Accept-Ranges: bytes
Expires: Fri, 23 Oct 2015 02:34:21 GMT
Cache-Control: max-age=31536000
X-Cache: MISS from shenzhen.gtimg.com
Content-Length: 13704
HTTP 头之后跟的 13704 字节就是PNG文件的内容。
HTTP/1.1 200 OK
Server: cws/3.8.3
Date: Thu, 23 Oct 2014 02:34:21 GMT
Content-Type: image/png
Content-Length: 13704
Connection: close
Last-Modified: Wed, 14 May 2014 02:03:23 GMT
ETag: "5372ceeb-3588"
Accept-Ranges: bytes
Expires: Fri, 23 Oct 2015 02:34:21 GMT
Cache-Control: max-age=31536000
X-Cache: MISS from shenzhen.gtimg.com
#14
发送了 i = 0 没有接收到数据 网关/》???
另外,你这个SOCKET连接是连接到哪个服务器哪个端口这个要考虑好,一般情况下是要连网关的
i = recv(sockfd, (void *)buf, 50, 0);
100 if(i == 0){
101 close(sockfd);
102 printf("read picture end\n");
103 return;
104 }
105 else{
106 fp = fopen(filename,"a");
107 fwrite(buf, 1, i, fp);
我直接接收然后保存到文件的 不对么??初学者捣鼓的 怎么做的????请指教
#15
这里只有fopen没有fclose呀?
#16
这里只有fopen没有fclose呀?
#include "downpicture.h"完整的代码 我还解析了 www.qq.com中新闻等url 可是发送请求不能下载 给我段错误 看来协议还得好好看看
void down_picture(char * url, const char * filename){
FILE * fp;
int sockfd;
int ret;
int ret1;
int i;
int h;
char url_get[50] = {0};
char url_host[100] = {0};
struct hostent * host;
struct sockaddr_in servaddr;
char str1[4096];
char buf[BUFSIZE];
char *str;
char str_get[4096];
socklen_t len;
fd_set t_set1;
struct timeval tv;
host = gethostbyname("www.qq.com");
fp = fopen(filename,"a");
if(fp < 0)
{
printf("open file fail...\n");
return;
}else
printf("file create success\n");
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
printf("socket error!\n");
exit(0);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr = *((struct in_addr*)host->h_addr);
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("connect error!\n");
exit(0);
}
printf("connect success \n");
memset(str1, 0, 4096);
sprintf(str1,"GET / HTTP/1.1\r\nHost:%s\r\nContent-Type=text/plain\r\nConnection:keep-alive\r\n\r\n","www.qq.com");
ret = send(sockfd,(void *)str1,strlen(str1),0);
if (ret < 0)
{
printf("send error %d,Error message'%s'\n",errno, strerror(errno));
exit(0);
}
else
{
printf("send success ,total send %d \n", ret);
}
while(1)
{
tv.tv_sec= 0;
tv.tv_usec= 0;
h= 0;
FD_ZERO(&t_set1);
FD_SET(sockfd, &t_set1);
h= select(sockfd +1, &t_set1, NULL, NULL, &tv);
if (h == 0) continue;
if (h < 0)
{
close(sockfd);
printf("some thing read error!\n");
return;
}
if (h > 0)
{
memset(buf, 0, 4095);
i= recv(sockfd, (void *)buf, 4092,0);
if (i==0)
{
strncpy(url_host, url, 21);
strcpy(url_get, &url[22]);
printf("%s\n%s\n\n", url_host, url_get);
memset(str_get, 0, 4096);
sprintf(str_get,"GET %s HTTP/1.0\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection:Close\r\n\r\n",url_get, "www.qq.com");
//sprintf(str_get,"GET %s HTTP/1.1\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection:keep-alive\r\n\r\n",filename, "http://mat1.gtimg.com");
ret1 = send(sockfd, (void *)str_get, strlen(str_get), 0);
if(ret1 < 0){
printf("resend error %d,%s", errno, strerror(errno));
exit(0);
}else
printf("resend success, total is:%d\nsockfd is %d\n\n", ret1, sockfd);
memset(buf, 0, 4095);
while(1){
i = recv(sockfd, (void *)buf, 50, 0);
if(i == 0){
close(sockfd);
printf("read picture end\n");
return;
}
else{
printf("%d\n\n", i);
fwrite(buf, 1, i, fp);
}
}
}
}
}
fclose(fp);
close(sockfd);
}
#17
自己解决啦 哈哈
#1
没人么 给自己顶!!
#2
人呢??
#3
难不成,发送的请求有点问题
#4
你是靠主动轮循去接收吗?具体的recv是系统函数还是你自己写的,你写的socket其它通讯是OK的吗?
#5
另外,你这个SOCKET连接是连接到哪个服务器哪个端口这个要考虑好,一般情况下是要连网关的
#6
难不成,发送的请求有点问题
第一次写有点乱
你看下
#include "downpicture.h"
void down_picture(char * url, const char * filename){
FILE * fp;
int sockfd;
int ret;
int ret1;
int i;
int h;
struct hostent * host;
struct sockaddr_in servaddr;
char str1[4096];
char buf[BUFSIZE];
char *str;
char str_get[4096];
socklen_t len;
fd_set t_set1;
struct timeval tv;
host = gethostbyname("www.qq.com");
if(fp < 0)
{
printf("open file fail...\n");
return;
}else
printf("file create success\n");
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
printf("socket error!\n");
exit(0);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr = *((struct in_addr*)host->h_addr);
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("connect error!\n");
exit(0);
}
printf("connect success \n");
memset(str1, 0, 4096);
sprintf(str1,"GET / HTTP/1.0\r\nHost:%s\r\nContent-Type=text/plain\r\nConnection:keep-alive\r\n\r\n","www.qq.com");
ret = send(sockfd,(void *)str1,strlen(str1),0);
if (ret < 0)
{
printf("send error %d,Error message'%s'\n",errno, strerror(errno));
exit(0);
}
else
{
printf("send success ,total send %d \n", ret);
}
while(1)
{
tv.tv_sec= 0;
tv.tv_usec= 0;
h= 0;
FD_ZERO(&t_set1);
FD_SET(sockfd, &t_set1);
h= select(sockfd +1, &t_set1, NULL, NULL, &tv);
if (h == 0) continue;
if (h < 0)
{
close(sockfd);
printf("some thing read error!\n");
return;
}
if (h > 0)
{
memset(buf, 0, 4095);
i= recv(sockfd, (void *)buf, 4092,0);
if (i==0)
{
memset(str_get, 0, 4096);
sprintf(str_get,"GET / HTTP/1.1\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection:Close\r\n\r\n",url);
//sprintf(str_get,"GET %s HTTP/1.1\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection:keep-alive\r\n\r\n",filename, "http://mat1.gtimg.com");
ret1 = send(sockfd, (void *)str_get, strlen(str_get), 0);
if(ret1 < 0){
printf("resend error %d,%s", errno, strerror(errno));
exit(0);
}else
printf("resend success, total is:%d\n", ret1);
memset(buf, 0, 4095);
while(1){
i = recv(sockfd, (void *)buf, 50, 0);
if(i == 0){
close(sockfd);
printf("read picture end\n");
return;
}
else{
fp = fopen(filename,"a");
fwrite(buf, 1, i, fp);
}
}
}
}
}
fclose(fp);
close(sockfd);
}
#7
你是靠主动轮循去接收吗?具体的recv是系统函数还是你自己写的,你写的socket其它通讯是OK的吗?
#8
另外,你这个SOCKET连接是连接到哪个服务器哪个端口这个要考虑好,一般情况下是要连网关的
#9
GET 请求格式不对呀
sprintf(str,
"GET /www/images/qq2012/guide_k_04.png HTTP/1.1\r\n"
"Host: mat1.gtimg.com\r\n"
"Connection: close\r\n"
"\r\n"
);
sprintf(str,
"GET /www/images/qq2012/guide_k_04.png HTTP/1.1\r\n"
"Host: mat1.gtimg.com\r\n"
"Connection: close\r\n"
"\r\n"
);
#10
网关/》???
另外,你这个SOCKET连接是连接到哪个服务器哪个端口这个要考虑好,一般情况下是要连网关的
#11
GET 请求格式不对呀
sprintf(str,
"GET /www/images/qq2012/guide_k_04.png HTTP/1.1\r\n"
"Host: mat1.gtimg.com\r\n"
"Connection: close\r\n"
"\r\n"
);
请求格式也改了 也确实接收到数据 可是我接收数据创建图片 却0字节
sprintf(str_get,"GET %s HTTP/1.0\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection:C lose\r\n\r\n",url_get, url_host);
90 //sprintf(str_get,"GET %s HTTP/1.1\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection: keep-alive\r\n\r\n",filename, "http://mat1.gtimg.com");
91 ret1 = send(sockfd, (void *)str_get, strlen(str_get), 0);
92 if(ret1 < 0){
93 printf("resend error %d,%s", errno, strerror(errno));
94 exit(0);
95 }else
96 printf("resend success, total is:%d\n", ret1);
97 memset(buf, 0, 4095);
98 while(1){
99 i = recv(sockfd, (void *)buf, 50, 0);
100 if(i == 0){
101 close(sockfd);
102 printf("read picture end\n");
103 return;
104 }
105 else{
106 fp = fopen(filename,"a");
107 fwrite(buf, 1, i, fp);
108 }
109 }
#12
网关/》???
另外,你这个SOCKET连接是连接到哪个服务器哪个端口这个要考虑好,一般情况下是要连网关的
#13
返回的HTTP报文你解析对了没有:
Content-Length: 13704
HTTP 头之后跟的 13704 字节就是PNG文件的内容。
HTTP/1.1 200 OK
Server: cws/3.8.3
Date: Thu, 23 Oct 2014 02:34:21 GMT
Content-Type: image/png
Content-Length: 13704
Connection: close
Last-Modified: Wed, 14 May 2014 02:03:23 GMT
ETag: "5372ceeb-3588"
Accept-Ranges: bytes
Expires: Fri, 23 Oct 2015 02:34:21 GMT
Cache-Control: max-age=31536000
X-Cache: MISS from shenzhen.gtimg.com
Content-Length: 13704
HTTP 头之后跟的 13704 字节就是PNG文件的内容。
HTTP/1.1 200 OK
Server: cws/3.8.3
Date: Thu, 23 Oct 2014 02:34:21 GMT
Content-Type: image/png
Content-Length: 13704
Connection: close
Last-Modified: Wed, 14 May 2014 02:03:23 GMT
ETag: "5372ceeb-3588"
Accept-Ranges: bytes
Expires: Fri, 23 Oct 2015 02:34:21 GMT
Cache-Control: max-age=31536000
X-Cache: MISS from shenzhen.gtimg.com
#14
发送了 i = 0 没有接收到数据 网关/》???
另外,你这个SOCKET连接是连接到哪个服务器哪个端口这个要考虑好,一般情况下是要连网关的
i = recv(sockfd, (void *)buf, 50, 0);
100 if(i == 0){
101 close(sockfd);
102 printf("read picture end\n");
103 return;
104 }
105 else{
106 fp = fopen(filename,"a");
107 fwrite(buf, 1, i, fp);
我直接接收然后保存到文件的 不对么??初学者捣鼓的 怎么做的????请指教
#15
这里只有fopen没有fclose呀?
#16
这里只有fopen没有fclose呀?
#include "downpicture.h"完整的代码 我还解析了 www.qq.com中新闻等url 可是发送请求不能下载 给我段错误 看来协议还得好好看看
void down_picture(char * url, const char * filename){
FILE * fp;
int sockfd;
int ret;
int ret1;
int i;
int h;
char url_get[50] = {0};
char url_host[100] = {0};
struct hostent * host;
struct sockaddr_in servaddr;
char str1[4096];
char buf[BUFSIZE];
char *str;
char str_get[4096];
socklen_t len;
fd_set t_set1;
struct timeval tv;
host = gethostbyname("www.qq.com");
fp = fopen(filename,"a");
if(fp < 0)
{
printf("open file fail...\n");
return;
}else
printf("file create success\n");
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
printf("socket error!\n");
exit(0);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr = *((struct in_addr*)host->h_addr);
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("connect error!\n");
exit(0);
}
printf("connect success \n");
memset(str1, 0, 4096);
sprintf(str1,"GET / HTTP/1.1\r\nHost:%s\r\nContent-Type=text/plain\r\nConnection:keep-alive\r\n\r\n","www.qq.com");
ret = send(sockfd,(void *)str1,strlen(str1),0);
if (ret < 0)
{
printf("send error %d,Error message'%s'\n",errno, strerror(errno));
exit(0);
}
else
{
printf("send success ,total send %d \n", ret);
}
while(1)
{
tv.tv_sec= 0;
tv.tv_usec= 0;
h= 0;
FD_ZERO(&t_set1);
FD_SET(sockfd, &t_set1);
h= select(sockfd +1, &t_set1, NULL, NULL, &tv);
if (h == 0) continue;
if (h < 0)
{
close(sockfd);
printf("some thing read error!\n");
return;
}
if (h > 0)
{
memset(buf, 0, 4095);
i= recv(sockfd, (void *)buf, 4092,0);
if (i==0)
{
strncpy(url_host, url, 21);
strcpy(url_get, &url[22]);
printf("%s\n%s\n\n", url_host, url_get);
memset(str_get, 0, 4096);
sprintf(str_get,"GET %s HTTP/1.0\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection:Close\r\n\r\n",url_get, "www.qq.com");
//sprintf(str_get,"GET %s HTTP/1.1\r\nHost:%s\r\nContent-Type=images/x-icon\r\nConnection:keep-alive\r\n\r\n",filename, "http://mat1.gtimg.com");
ret1 = send(sockfd, (void *)str_get, strlen(str_get), 0);
if(ret1 < 0){
printf("resend error %d,%s", errno, strerror(errno));
exit(0);
}else
printf("resend success, total is:%d\nsockfd is %d\n\n", ret1, sockfd);
memset(buf, 0, 4095);
while(1){
i = recv(sockfd, (void *)buf, 50, 0);
if(i == 0){
close(sockfd);
printf("read picture end\n");
return;
}
else{
printf("%d\n\n", i);
fwrite(buf, 1, i, fp);
}
}
}
}
}
fclose(fp);
close(sockfd);
}
#17
自己解决啦 哈哈