'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. When I create a Gstreamer pipeline using udpsink, VLC can play the stream. I want to create my own program that would receive udp packets from Gstreamer udpsink. The UDP connection code I wrote just waits on recvfrom method. 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;


Solution 1:[1]

You are misisng the call to bind, so that you listen on the specific address/port:

// 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. In your current code, the server information is not used.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AndresR