我正在尝试使用C套接字下载mp4视频。文件已成功下载但当我尝试播放时无效。文件与原始文件大小相同。但他们的校验和是不同的。但是这段代码适用于pdf文件。 我很感激并感谢您的负担。
int main() {
int socket_desc;
char *message;
char server_reply[100000];
char *file_path = "file.mp4";
ssize_t total_len = 0;
char *ip = NULL;
FILE *file = NULL;
struct sockaddr_in server;
//Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
printf("dCould not create socket");
}
server.sin_addr.s_addr = inet_addr("195.154.200.24");
server.sin_family = AF_INET;
server.sin_port = htons(80);
if (connect(socket_desc, (struct sockaddr *) &server, sizeof(server)) < 0) {
printf("connect error");
return 1;
}
message = "GET /upload_file/367/382/Indian%20Pop%20Hit%20Video%20Songs/Indian%20Pop%20Video%20Songs%202016%20-%20MP4/Jammin%20-%20Yaara%20-%20A%20R%20Rahman%20-%20Video%20MP4.mp4 HTTP/1.1\r\nHost: dl.pagal.link\r\n\r\n";
if (send(socket_desc, message, strlen(message), 0) < 0) {
printf("Send failed");
return 1;
}
remove( file_path );
file = fopen( file_path , "ab");
if (file == NULL) {
printf("File %s could not opened", file_path);
return 1;
}
while (1) {
ssize_t received_len = recv(socket_desc, server_reply, sizeof(server_reply), 0);
if (received_len < 0) {
printf("Received failed\n");
return -1;
}
if (received_len == 0) {
break;
}
fwrite(server_reply, (size_t) received_len, 1, file);
}
fclose(file);
return 0;
}