欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > Linux-客户端访问mjpeg-stream的web网站,保存图像

Linux-客户端访问mjpeg-stream的web网站,保存图像

2025/7/9 22:21:36 来源:https://blog.csdn.net/m0_73939014/article/details/144203333  浏览:    关键词:Linux-客户端访问mjpeg-stream的web网站,保存图像

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>#define SERVER_IP "192.168.85.207"
#define PORT 8080
#define BUFFER_SIZE 1024/** Generate an HTTP GET request for a static snapshot.* This function creates a properly formatted HTTP GET request string* to retrieve a static snapshot from an mjpg - streamer server.* Return: A pointer to the generated request string.*/
char* generate_get_request() {char* request = (char*)malloc(1024);if (request == NULL) {perror("malloc error");exit(EXIT_FAILURE);}sprintf(request, "GET /?action=snapshot HTTP/1.1\r\n""Host: %s:%d\r\n""User - Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0\r\n""Accept: image/jpeg\r\n""Connection: close\r\n\r\n", SERVER_IP, PORT);return request;
}/** Read data from the server and save it as a file.* This function handles receiving data from the server and writing it to a file.* It first skips the HTTP header information and then saves the image data.* Parameters:*      - sockfd: The socket file descriptor connected to the server.*/
void save_image_from_server(int sockfd) {FILE *fp;char buffer[BUFFER_SIZE];size_t bytes_read;/* Open a file to write the image data */fp = fopen("received_image.jpg", "wb");if (fp == NULL) {perror("Error opening file");close(sockfd);exit(EXIT_FAILURE);}/* Skip the HTTP header information */char* http_header_end;do {bytes_read = recv(sockfd, buffer, BUFFER_SIZE, 0);if (bytes_read <= 0) {perror("Error receiving HTTP header");fclose(fp);close(sockfd);exit(EXIT_FAILURE);}printf("Received bytes in header loop: %zu\n", bytes_read);http_header_end = strstr(buffer, "\r\n\r\n");} while (http_header_end == NULL);/* Start writing the image data (skip the HTTP header) */fwrite(http_header_end + 4, 1, bytes_read - (http_header_end + 4 - buffer), fp);/* Continue receiving and saving the remaining image data */while ((bytes_read = recv(sockfd, buffer, BUFFER_SIZE, 0)) > 0) {printf("Received bytes of image data: %zu\n", bytes_read);fwrite(buffer, 1, bytes_read, fp);}if (bytes_read < 0) {perror("Error receiving image data");}fclose(fp);
}int main() {int sockfd;struct sockaddr_in server_addr;char* request;/* Create a socket */sockfd = socket(AF_INET, SOCK_STREAM, 0);if (sockfd < 0) {perror("Error creating socket");exit(EXIT_FAILURE);}printf("Socket created.\n");/* Set up the server address structure */server_addr.sin_family = AF_INET;server_addr.sin_port = htons(PORT);server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);/* Connect to the server */if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {perror("Error connecting to server");close(sockfd);exit(EXIT_FAILURE);}printf("Connected to the server.\n");/* Generate an HTTP GET request for a static snapshot */request = generate_get_request();/* Send the request */if (send(sockfd, request, strlen(request), 0) < 0) {perror("Error sending request");free(request);close(sockfd);exit(EXIT_FAILURE);}free(request);printf("Request sent.\n");/* Get and save the image from the server */save_image_from_server(sockfd);/* Close the socket connection */close(sockfd);printf("Socket closed.\n");return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词