简体   繁体   English

如何创建 UDP 套接字连接以从 gstreamer udpsink 接收数据?

[英]How to create UDP socket connection to receive data from gstreamer udpsink?

I want to create a UDP socket connection to receive data from GStreamer udpsink.我想创建一个 UDP 套接字连接来接收来自 GStreamer udpsink 的数据。 When I create a Gstreamer pipeline using udpsink, VLC can play the stream.当我使用 udpsink 创建 Gstreamer 管道时,VLC 可以播放 stream。 I want to create my own program that would receive udp packets from Gstreamer udpsink.我想创建自己的程序来接收来自 Gstreamer udpsink 的 udp 数据包。 The UDP connection code I wrote just waits on recvfrom method.我写的 UDP 连接代码只是等待 recvfrom 方法。 Can you please help?你能帮忙吗?

    // Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
    perror("socket creation failed");
    exit(EXIT_FAILURE);
}

memset(&servaddr, 0, sizeof(servaddr));
   
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
   
int n, len;
int loop = 10;
do
{
       
n = recvfrom(sockfd, (char *)buffer, MAXLINE, 
            MSG_WAITALL, (struct sockaddr *) &servaddr,
            &len);
buffer[n] = '\0';
printf("Server : %s\n", buffer);
} while (--loop);
close(sockfd);
return 0;

You are misisng the call to bind , so that you listen on the specific address/port:您错过了对bind的调用,以便您在特定地址/端口上监听:

// Bind the socket with the server address 
if ( bind(sockfd, (const struct sockaddr *)&servaddr,  
        sizeof(servaddr)) < 0 ) 
{ 
    perror("bind failed"); 
    exit(EXIT_FAILURE); 
} 

In the call to recvfrom , servaddr is filled with the remote address, it is an output argument.在对recvfrom的调用中, servaddr填充了远程地址,它是一个 output 参数。 In your current code, the server information is not used.在您当前的代码中,未使用服务器信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM