SSDP UDP broadcast ...

I got the this simple SSDP search example working. I was not able to get all root UPnP devices that I got with the UPnP Device spy?

Is this kind of timing problem?
Is there any simple ansi-c BSD based example that works properlly?
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#define SSDP_MULTICAST      ""
#define SSDP_PORT           1900
int main (int argc, const char * argv[])
	int sock;
	size_t ret;
	unsigned int socklen;
	struct sockaddr_in sockname;
	struct sockaddr clientsock;
	struct hostent *hostname;
	char data[] = 
    "M-SEARCH * HTTP/1.1\r\n"
    "Man: \"ssdp:discover\"\r\n"
	char buffer[RESPONSE_BUFFER_LEN];
	unsigned int len = RESPONSE_BUFFER_LEN;
	fd_set fds;
	struct timeval timeout;
	hostname = gethostbyname(SSDP_MULTICAST);
	hostname->h_addrtype = AF_INET;
	if((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1){
		printf("err: socket() failed");
		return -1;
	memset((char*)&sockname, 0, sizeof(struct sockaddr_in));
	sockname.sin_addr.s_addr=*((unsigned long*)(hostname->h_addr_list[0]));
	ret=sendto(sock, data, strlen(data), 0, (struct sockaddr*) &sockname, 
			   sizeof(struct sockaddr_in));
	if(ret != strlen(data)){
		return -1;
	/* Get response */
	FD_SET(sock, &fds);
	if(select(sock+1, &fds, NULL, NULL, &timeout) < 0){
		return -1;
	if(FD_ISSET(sock, &fds)){
		if((len = recvfrom(sock, buffer, len, MSG_PEEK, 
						   &clientsock, &socklen)) == (size_t)-1){
			printf("err: recvfrom");
			return -1;
		/* Check the HTTP response code */
		if(strncmp(buffer, "HTTP/1.1 200 OK", 12) != 0){
			printf("err: ssdp parsing ");
			return -1;
		return 0;
		printf("err: no ssdp answer");
		return -1;

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Examples here:

Suggest one has to reissue multicast request until device list stops growing.
koleto115Author Commented:
1. I will check the link, sound promising.
2. You mean I should run the function on a loop ... I tryied already but somehow i still get my rooter and no one other device and i have another one for sure.
It looks like a timing problem.

I am not the exper on the matter. I just looked how Gnome does things you asked for.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
koleto115Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.