Learn to use http requests to download when encountered a bit of trouble, ask for answers

int main()
{
    int tcp_socket = socket(AF_INET,SOCK_STREAM,0);
    if(tcp_socket == -1)
    {
        perror("创建失败");
        return -1;
    }
 
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8886);
    addr.sin_addr.s_addr = inet_addr("47.107.240.45");
    int ret = connect(tcp_socket,(struct sockaddr*)&addr,sizeof(addr));
    if(ret == -1)
    {
        perror("连接失败");
    }
 
  
    char *http_request = "GET /group1/M00/15/20/wKgP3GJVOMGAUZ25AABsxJzJJCI270.zip?token=null&ts=null&filename=cJSON.zip HTTP/1.1rnHost:vm.yueqian.com.cn:8886rnConnection: closernrn";
    
    write(tcp_socket,http_request,strlen(http_request));
 
 
        char read_buf[1024] = {0};
        int ret1 = read(tcp_socket,read_buf,sizeof(read_buf));
     
        printf("%dn",ret1);
        printf("文件大小为%dn文件头%s",ret1,read_buf);
     
    
    return 0;
 
}
 

What this code prints when it runs is
enter image description here
The read_buf[1024 * 1024] in the above code changes the size and runs the print out
enter image description here

You can see that the read return value is different but why is it that the print is the same, the loop read found that there is actually content behind, but the 1024 1024 size array is still the same as the 1024 size array print conten

New contributor

nijiasuo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Khám phá các thẻ bài đăng