Multicast - Winsockets - Error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Multicast - Winsockets - Error

  1. #1
    Join Date
    May 2006
    Posts
    4

    Question Multicast - Winsockets - Error 10049

    please, could anyone help me?

    I have following code where I need multicast connection using Source-specific multicast. When I call joinGroup(...) on setsockopt I got error 1049 which mean:

    Winsock error 10049 typically occurs when you attempt to create a socket with an address not on this machine. For example if you have MDaemon running on a machine with an IP address of 192.168.0.1 and you attempt to bind MDaemon to 192.168.0.100 you will receive this error message.

    I am using:
    Code:
    	char* g_ip_address = "232.1.1.1";
    	char* s_ip_address = "147.229.146.235";
    Where 147.229.146.235 is my own IP address.

    Has anyone Idea where the problem could be?
    Or does anyone know about any (working) example how to use Source-specific Multicast?
    OS: Windows Profesional, 32-bit, Visual C++ Studio 2005

    ---------------------------------------------------------------------------
    timeserv.cpp (4.8 KB)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #include <sys/types.h> 
    #include <ctype.h> 
    #include <winsock2.h> 
    #include <ws2tcpip.h>
    
    
    #pragma comment(lib, "ws2_32.lib") 
    
    void usage(void);
    
    int joinGroup(SOCKET sd
    	,struct ip_mreq_source *mc_source_address
    	,char *g_ip_address
    	,char *s_ip_address
    	) {
    	struct sockaddr_in *group  =(struct sockaddr_in*)&(mc_source_address->imr_multiaddr);
        struct sockaddr_in *source =(struct sockaddr_in*)&(mc_source_address->imr_sourceaddr);
    	
    	/* Clear group_source_req*/
    	memset((void *)mc_source_address, 0, sizeof(struct ip_mreq_source));
    	mc_source_address->imr_interface.s_addr = 0;
    
    	/* Group IP ADDRESS */
    	group->sin_family = AF_INET;
    	group->sin_addr.s_addr = inet_addr(g_ip_address);
    	group->sin_port = htons(0);
       
        /* Source IP ADDRESS */
        source->sin_family = AF_INET;
    	source->sin_addr.s_addr = inet_addr(s_ip_address);
        source->sin_port = htons(0);
    	
    	/* JOIN GROUP - (S,G) */
    	int ret = setsockopt(sd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
    					(char *)&mc_source_address, sizeof(struct ip_mreq_source));
    	if(ret == -1){
            printf("setsockopt() IP_ADD_MEMBERSHIP, Err: %d\n",
                                 WSAGetLastError()); 
    		closesocket(sd);
    		WSACleanup();
    		exit(0);
    	}
    	return ret;
    }
    
    
    void init_localAddress(SOCKET sockd, struct sockaddr_in *addr){
    	struct hostent *hp;					/* Information about this computer */
    	char host_name[256];				/* Name of the server */
    
    	/* Get host name of this computer */
    	gethostname(host_name, sizeof(host_name));
    	hp = gethostbyname(host_name);
    
    	/* Check for NULL pointer */
    	if (hp == NULL) {
    		fprintf (stderr, "Could not get host name() failed, Err: %d\n", WSAGetLastError()); 
    		closesocket(sockd);
    		WSACleanup();
    		exit(0);
    	}
    	fprintf (stderr, "HOSTNAME: %s\n", host_name); 
    	/* Assign the address */
    	addr->sin_addr.S_un.S_un_b.s_b1 = hp->h_addr_list[0][0];
    	addr->sin_addr.S_un.S_un_b.s_b2 = hp->h_addr_list[0][1];
    	addr->sin_addr.S_un.S_un_b.s_b3 = hp->h_addr_list[0][2];
    	addr->sin_addr.S_un.S_un_b.s_b4 = hp->h_addr_list[0][3];
    }
    
    int main(int argc, char **argv) {
    	WSADATA w;							/* Used to open windows connection */
    	int port_number;					/* Port number to use */
    	int a1, a2, a3, a4;					/* Components of address in xxx.xxx.xxx.xxx form */
    	SOCKET sd;							/* Socket descriptor of server */
    	struct sockaddr_in bindaddr;		/* Information about the server */
    
    	/* Interpret command line */
    	if (argc == 2) {
    		/* Use local address */
    		if (sscanf_s(argv[1], "%u", &port_number) != 1) {
    			usage();
    		}
    	}
    	else if (argc == 3) {
    		/* Copy address */
    		if (sscanf_s(argv[1], "%d.%d.%d.%d", &a1, &a2, &a3, &a4) != 4) {
    			usage();
    		}
    		if (sscanf_s(argv[2], "%u", &port_number) != 1) {
    			usage();
    		}
    	} else {
    		usage();
    	}
    
    	WORD wVersionRequested; 
    	wVersionRequested = MAKEWORD( 2, 0 );
    	/* Open windows connection */
    	if (WSAStartup(wVersionRequested, &w) != 0)
    	{
    		fprintf (stderr, "Could not open Windows connection, Err: %d\n", WSAGetLastError()); 
    		exit(0);
    	}
    
    	/* Open a datagram socket */
    	sd = socket(AF_INET, SOCK_DGRAM, 0);
    	if (sd == INVALID_SOCKET)
    	{
    		fprintf (stderr, "Could not create socket, Err: %d\n", WSAGetLastError()); 
    		WSACleanup();
    		exit(0);
    	}
    
    	/* Clear out server struct */
    	memset((void *)&bindaddr, '\0', sizeof(struct sockaddr_in));
    
    	/* Set family and port */
    	bindaddr.sin_family = AF_INET;
    	bindaddr.sin_port = htons(port_number);
    	bindaddr.sin_addr.s_addr = htonl(INADDR_ANY); 
    
    	/* Bind address to socket */
    	if (bind(sd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) == -1)
    	{
    		fprintf (stderr, "bind() failed, Err: %d\n", WSAGetLastError()); 
    		closesocket(sd);
    		WSACleanup();
    		exit(0);
    	}
    	
    
    	char* g_ip_address = "232.1.1.1";
    	char* s_ip_address = "147.229.146.235";
    	struct ip_mreq_source group_source_req; 
    
    	joinGroup(sd, &group_source_req, g_ip_address, s_ip_address);
    
    	/* Print out server information */
    	printf("Server running on %u.%u.%u.%u\n", (unsigned char)bindaddr.sin_addr.S_un.S_un_b.s_b1,
    											  (unsigned char)bindaddr.sin_addr.S_un.S_un_b.s_b2,
    											  (unsigned char)bindaddr.sin_addr.S_un.S_un_b.s_b3,
    											  (unsigned char)bindaddr.sin_addr.S_un.S_un_b.s_b4);
    	printf("Press CTRL + C to quit\n");
    
    
        /* SET DESTINATION PORT*/
    	struct sockaddr_in *group; 
    	group =(struct sockaddr_in*)&(group_source_req.imr_multiaddr);
        group->sin_port = htons(port_number); 
    	
    	while(1) {
    		printf(".\n");
            sendto(sd, "Hello World",(int)strlen("Hello World"), 0,
    			(struct sockaddr *)group, sizeof(struct sockaddr_in));
            Sleep(1000);
        } 
    
    	closesocket(sd);
    	WSACleanup();
    
    	return 0;
    }
    
    void usage(void)
    {
    	fprintf(stderr, "timeserv [server_address] port\n");
    	exit(0);
    }
    In attachment is also included working UNIX equivalent with:
    Code:
      /* Group is 232.1.1.1 */
        group->sin_family = AF_INET;
        inet_aton(g_addr_s, &group->sin_addr);
        group->sin_port = 0;
        
        /* Source is 10.1.20.9 */
        source->sin_family = AF_INET;
        inet_aton(s_addr_s,&source->sin_addr);
        source->sin_port = 0;
    
        // PIM - SSM
        res = setsockopt(con->fd,SOL_IP,MCAST_JOIN_SOURCE_GROUP, &(con->group_source_req),sizeof(con->group_source_req));
    multicast.c (2.7 KB) , sender.c (786 Bytes) , multicast.h (1.6 KB) , receiver.c (571 Bytes)
    Attached Files Attached Files
    Last edited by radone; May 16th, 2006 at 07:51 AM.

  2. #2
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,019

    Re: Multicast - Winsockets - Error

    [ Redirected thread ]

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,553

    Re: Multicast - Winsockets - Error

    I frankly have only general familiarity with multicast programming, so what I have to say might not help much.

    10049 is WSAEADDRNOTAVAIL. According to MSDN, "Multicast Socket Option Behavior" at http://msdn.microsoft.com/library/en...n_behavior.asp , when this error is returned from setsockopt(... IP_ADD_SOURCE_MEMBERSHIP ... ), it means that there is a conflicting socket option already in place:
    Do not call IP_ADD_SOURCE_MEMBERSHIP (or, IP_ADD_MEMBERSHIP) with the same group/source pair previously called with IP_ADD_SOURCE_MEMBERSHIP.
    I think you are already familiar with the MSDN sites that are specific to multicast programming, since your code is similar to the code given there, but to be certain:

    "Multicast Programming" at http://msdn.microsoft.com/library/en...rogramming.asp
    "Reliable Multicast Programming (PGM)" at http://msdn.microsoft.com/library/en...ming__pgm_.asp

    Finally, is there a reason that WSAStartup is called at version 2.0 instead of 2.2? It might be helpful to call it at the most current version (i.e., wVersionRequested = MAKEWORD( 2, 2 ); ).

    Mike

  4. #4
    Join Date
    Jun 2009
    Posts
    1

    Re: Multicast - Winsockets - Error

    i am facing exact same problem.
    Was your problem solved
    i would be very thank ful for any pointers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center