Like Tree1Likes
  • 1 Post By tabstop

Trying to receive from a multicast address

This is a discussion on Trying to receive from a multicast address within the C++ Programming forums, part of the General Programming Boards category; Hi, I've been trying to write up a program that sends and receives 'tweets' using multicast. I've got the sending ...

  1. #1
    jet
    jet is offline
    Registered User
    Join Date
    May 2011
    Posts
    3

    Trying to receive from a multicast address

    Hi,

    I've been trying to write up a program that sends and receives 'tweets' using multicast. I've got the sending part working but for some reason I can't seem to get the receiving part working.
    For receiving this is what I've got:
    insert
    Code:
    void receiveTweets(void) {
    	int status;
    	// open a socket
       SOCKET s = socket(AF_INET, SOCK_DGRAM, 0);
       struct sockaddr_in receive;
       receive.sin_family = AF_INET;
       receive.sin_addr.s_addr = inet_addr("224.28.99.66");
       receive.sin_addr.s_addr = INADDR_ANY;
       receive.sin_port = htons(33118);
    
    	status = bind(s, (struct sockaddr *)&receive, sizeof(struct sockaddr_in));
    
       if(status != 0) 
    	{
    		printf("Bind unsuccessful");
    	}
    	else 
    	{
    
    		struct ip_mreq mreq; //PROVIDE MULTICAST GROUP INFO
    		mreq.imr_multiaddr.s_addr = inet_addr("224.28.99.66");
    		mreq.imr_interface.s_addr = INADDR_ANY;
    
    setsockopt (s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mreq, sizeof(mreq));
    		
    	receive.sin_family = AF_INET;
    	receive.sin_addr.s_addr = inet_addr("224.28.99.66");
    		
    	char buff[MTU];
    	int bytes = -1;
    
    		while (1)
    		{
    			status = recvfrom(s, buff, strlen(buff), 0, (struct sockaddr*)&receive, (int *)sizeof(struct sockaddr_in));
    			if(status > 0) 
    			{
    				printf("Received: \"%s\"", buff);
    			}
    		}	
    
    	}
    	closesocket(s);
    
       return;
    }
    This code seems to have recvfrom return -1 (the errror) but I can't seem to see where I'm going wrong. Can anyone help me??

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Trying to receive strlen(buff) characters could be interesting, because who has any idea what size that is? Perhaps you meant sizeof instead. And why not check for the error you get with errno, if you do actually receive -1?

  3. #3
    jet
    jet is offline
    Registered User
    Join Date
    May 2011
    Posts
    3
    i used visual studio to debug it and it kept giving the value of status -1. i also tried using int WSAGetLastError(void); and the error codes kept changing. as for strlen(buff) i thought that recvfrom required the length of the buffer..

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It does require the length of the buffer. strlen is a nice thing to have, but it is not the length of the buffer. The length of the buffer is how many bytes of memory you have set aside for the buffer, and does not change just by putting something in it. strlen, however, is the length of the string you have placed in the buffer; and if you haven't (yet) placed a string in the buffer, then you could get a very large (or very negative, if you're really unlucky) value for strlen.

    If you're getting many error codes from WSAGetLastError, then that mean you have many wrong things. Or you might just be getting a lot of EAGAIN. You need to look at what the codes mean.
    Salem likes this.

  5. #5
    jet
    jet is offline
    Registered User
    Join Date
    May 2011
    Posts
    3
    ok great thanks for your help...i'll give it a go..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IPv6 multicast client fails to receive server response
    By perfect in forum Networking/Device Communication
    Replies: 10
    Last Post: 06-21-2009, 11:30 PM
  2. Missing first few multicast messages in udp.
    By lovecoding in forum C Programming
    Replies: 3
    Last Post: 03-04-2009, 02:53 PM
  3. Reliable Multicast
    By markcole in forum Networking/Device Communication
    Replies: 0
    Last Post: 04-23-2007, 07:17 AM
  4. Multicast Quiestion
    By arron in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-14-2005, 11:17 PM
  5. IP Multicast
    By Hunter2 in forum Networking/Device Communication
    Replies: 0
    Last Post: 01-07-2004, 02:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21