linux系统用C语言写个视频服务器,求老前辈们指导
我的c语言只有二级水平,班门弄斧了。
似乎,linux本身就有几个不错的开源视频服务器,
如果要求不高的话可以直接使用他们的;
如果他们的不能达到要求,可以使用c些一些代码做转换,然后使用popen,system之类的调用;
或者,借鉴开源的视频服务器代码自己深度加工;
gstreamer本身就是专门处理视频流的,可以看看能不能用上。
/ File: serverc /
#include <stdioh>
#include <sys/typesh>
#include <sys/socketh>
#include <netinet/inh>
int main(int argc, char argv)
{
int fd;
int address_len;
struct sockaddr_in address;
//建立套接口
fd = socket(AF_INET, SOCK_DGRAM, 0); //SOCK_DGRAM
//绑定地址和端口
bzero(&address, sizeof(address));
addresssin_family = AF_INET;
addresssin_addrs_addr = htonl(INADDR_ANY);
addresssin_port = htons(1234);
address_len = sizeof(address);
bind(fd, (struct sockaddr )&address, address_len);
while(1) {
struct sockaddr_in client_address;
socklen_t len = sizeof(client_address);
int n;
char line[80];
printf("waiting");
fflush(stdout);
//接收数据
n = recvfrom(fd, line, 80, 0,
(struct sockaddr )&client_address, &len);
printf("server received %d:%s", n, line);
//发送数据
sendto(fd, line, n, 0,
(struct sockaddr )&client_address, len);
}
}
/ File: clientc /
#include <sys/typesh>
#include <sys/socketh>
#include <netinet/inh>
int main(int argc, char argv)
{
int fd;
struct sockaddr_in address;
int address_len;
char line[80] = "Client to Server string!\n";
int n;
//建立套接口
fd = socket(AF_INET, SOCK_DGRAM, 0);//AF_INET和SOCK_DGRAM的组合对应UDP协议
//联接
bzero(&address, sizeof(address));
addresssin_family = AF_INET;
addresssin_addrs_addr = inet_addr("1931931961");
addresssin_port = htons(1234);
address_len = sizeof(address);
//发送数据
sendto(fd, line, strlen(line)+1, 0,
(struct sockaddr )&address, sizeof(address));
//接收数据
n = recvfrom(fd, line, 80, 0, NULL, NULL);
printf("received %d:%s", n, line);
}
仔细读一下,最好下次能自己写^_^,起码要弄懂原理
0条评论