C 我能';无法获得完整的网页

C 我能';无法获得完整的网页,c,http,C,Http,我想做http请求,但它没有下载整个页面。从任何大网页中只能看到7300字节或更少 如果我尝试小网页它下载完整 我尝试了不同的旗帜,但仍然找不到我的错误 #define POSIX_C_SOURCE >= 200112L #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <unistd.h> #include <stdio.h> #

我想做http请求,但它没有下载整个页面。从任何大网页中只能看到7300字节或更少 如果我尝试小网页它下载完整 我尝试了不同的旗帜,但仍然找不到我的错误

#define POSIX_C_SOURCE >= 200112L
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
struct addrinfo hints, *res;
int sockfd;
char buf[25056];
int byte_count;

 memset(&hints, 0,sizeof hints);
hints.ai_family=AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo("man7.org","80", &hints, &res);
sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
printf("Connecting...\n");
connect(sockfd,res->ai_addr,res->ai_addrlen);
printf("Connected!\n");
char *header = "GET /linux/man-pages/man2/recv.2.html HTTP/1.1\r\nHost:         
man7.org\r\n\r\n";
send(sockfd,header,strlen(header),0);
printf("GET Sent...\n");
//all right ! now that we're connected, we can receive some data!

byte_count = recv(sockfd,buf,sizeof(buf),0);
printf("recv()'d %d bytes of data in buf\n",byte_count);
printf("%.*s",byte_count,buf); // <-- give printf() the actual data size

return 0;
}

正如注释中所指出的,您必须反复地
recv
,直到返回值小于或等于零。同样在这样一个简单的情况下,我建议发送HTML1.0请求以避免分块回答。请尝试以下代码:

#define POSIX_C_SOURCE >= 200112L
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    struct addrinfo hints, *res;
    int sockfd;
    char buf[65536];
    int n, byte_count;

    memset(&hints, 0,sizeof hints);
    hints.ai_family=AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    getaddrinfo("man7.org","80", &hints, &res);
    sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
    printf("Connecting...\n");
    connect(sockfd,res->ai_addr,res->ai_addrlen);
    printf("Connected!\n");
    char *header = "GET /linux/man-pages/man2/recv.2.html HTTP/1.0\r\nHost:man7.org\r\n\r\n";
    send(sockfd,header,strlen(header),0);
    printf("GET Sent...\n");
//all right ! now that we're connected, we can receive some data!

    byte_count = n = 0;
    do {
        byte_count += n;
        n = recv(sockfd,buf+byte_count,sizeof(buf)-byte_count,0);
    } while (n > 0);
    printf("recv()'d %d bytes of data in buf\n",byte_count);
    printf("%.*s",byte_count,buf); // <-- give printf() the actual data size

    return 0;
}
#定义POSIX_C_源>=200112L
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
结构addrinfo提示,*res;
int-sockfd;
char-buf[65536];
int n,字节计数;
memset(提示和提示,0,提示大小);
hits.ai_family=AF_unsec;
hits.ai_socktype=SOCK_流;
getaddrinfo(“man7.org”、“80”和提示以及res);
sockfd=socket(res->ai_系列,res->ai_socktype,res->ai_协议);
printf(“连接…\n”);
连接(sockfd,res->ai_addr,res->ai_addrlen);
printf(“已连接!\n”);
char*header=“GET/linux/man pages/man2/recv.2.html HTTP/1.0\r\n主机:man7.org\r\n\r\n”;
发送(sockfd,头,strlen(头),0);
printf(“获取发送…\n”);
//好的!现在我们连接好了,我们可以接收一些数据了!
字节计数=n=0;
做{
字节计数+=n;
n=recv(sockfd,buf+字节计数,sizeof(buf)-字节计数,0);
}而(n>0);
printf(“recv()'d%d字节数据,单位为buf\n”,字节计数);

printf(“%s”,byte_count,buf);//正如注释中所指出的,您必须反复执行
recv
,直到返回值小于或等于零。同样在这种简单的情况下,我建议发送HTML 1.0请求以避免分块回答。请尝试以下代码:

#define POSIX_C_SOURCE >= 200112L
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    struct addrinfo hints, *res;
    int sockfd;
    char buf[65536];
    int n, byte_count;

    memset(&hints, 0,sizeof hints);
    hints.ai_family=AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    getaddrinfo("man7.org","80", &hints, &res);
    sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
    printf("Connecting...\n");
    connect(sockfd,res->ai_addr,res->ai_addrlen);
    printf("Connected!\n");
    char *header = "GET /linux/man-pages/man2/recv.2.html HTTP/1.0\r\nHost:man7.org\r\n\r\n";
    send(sockfd,header,strlen(header),0);
    printf("GET Sent...\n");
//all right ! now that we're connected, we can receive some data!

    byte_count = n = 0;
    do {
        byte_count += n;
        n = recv(sockfd,buf+byte_count,sizeof(buf)-byte_count,0);
    } while (n > 0);
    printf("recv()'d %d bytes of data in buf\n",byte_count);
    printf("%.*s",byte_count,buf); // <-- give printf() the actual data size

    return 0;
}
#定义POSIX_C_源>=200112L
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
结构addrinfo提示,*res;
int-sockfd;
char-buf[65536];
int n,字节计数;
memset(提示和提示,0,提示大小);
hits.ai_family=AF_unsec;
hits.ai_socktype=SOCK_流;
getaddrinfo(“man7.org”、“80”和提示以及res);
sockfd=socket(res->ai_系列,res->ai_socktype,res->ai_协议);
printf(“连接…\n”);
连接(sockfd,res->ai_addr,res->ai_addrlen);
printf(“已连接!\n”);
char*header=“GET/linux/man pages/man2/recv.2.html HTTP/1.0\r\n主机:man7.org\r\n\r\n”;
发送(sockfd,头,strlen(头),0);
printf(“获取发送…\n”);
//好的!现在我们连接好了,我们可以接收一些数据了!
字节计数=n=0;
做{
字节计数+=n;
n=recv(sockfd,buf+字节计数,sizeof(buf)-字节计数,0);
}而(n>0);
printf(“recv()'d%d字节数据,单位为buf\n”,字节计数);

printf(“%s”,byte_count,buf);//可能相关:关于:
byte_count=recv(sockfd,buf,sizeof(buf),0);
以太网数据包的大小有限,因此需要在循环中,其中:始终检查返回值,如果为0,则将输入数据附加到输入缓冲区(这通常是通过操纵第二个和第三个参数来完成的,因此不需要执行单独的复制。请始终检查来自
socket()
connect()
send()
recv()
(等)的返回值,以确保操作成功。可能相关:关于:
byte\u count=recv(sockfd,buf,sizeof(buf),0);
以太网数据包的大小有限,因此需要在循环中,其中:始终检查返回值,如果为0,则将输入数据附加到输入缓冲区(这通常是通过操纵第二个和第三个参数来完成的,因此不需要执行单独的复制。始终检查
socket()
connect()
send()
recv()
(等)中的返回值,以确保操作成功。