linux中c语言实现实时视频传输完整代码
时间: 2023-01-30 12:34:35 浏览: 102
这是一个使用 C 语言在 Linux 系统下实现实时视频传输的完整代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <pthread.h>
#include <signal.h>
#define PORT 8000
#define BUFSIZE 1024
int sockfd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
char buf[BUFSIZE];
int len;
int n;
void *send_video(void *arg)
{
// 初始化视频文件
FILE *fp = fopen("video.mp4", "r");
if (fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
// 读取视频文件并发送
while ((n = fread(buf, 1, BUFSIZE, fp)) > 0)
{
printf("Sending %d bytes\n", n);
sendto(sockfd, buf, n, 0, (struct sockaddr *)&client_addr, len);
}
// 关闭文件和套接字
fclose(fp);
close(sockfd);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
// 创建套接字
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
printf("Error creating socket\n");
exit(1);
}
// 绑定服务器地址
memset((char *)&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORT);
if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
printf("Error binding\n");
exit(1);
}
// 初始化客户端地址
len = sizeof(client_addr);
memset((char *)&client_addr, 0, sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = inet_addr("127.0.
阅读全文