tags:

views:

79

answers:

2

Hi,

I have developed one server pgm for multicasting in C++, when i register the address and after that when i set the socket option using setsockopt it returns me -1

If this option is not supported in setsockopt then how i can go for multicasting in C++

My code is below :

#include"winsock.h"
#include<iostream>
#include<conio.h>

using namespace std;

#define MYPORT 9009 // the port users will be connecting to






#define MAXBUFSIZE 65536 // Max UDP Packet size is 64 Kbyte

int main()
{


    WSADATA wsaData;
    WSAStartup(MAKEWORD(2,2), &wsaData);

   int sock, status, socklen;
   char buffer[MAXBUFSIZE];
   struct sockaddr_in saddr;
   struct ip_mreq imreq;

   // set content of struct saddr and imreq to zero
   memset(&saddr, 0, sizeof(sockaddr_in));
   memset(&imreq, 0, sizeof(ip_mreq));

   // open a UDP socket
   sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
   if ( sock < 0 )
     perror("Error creating socket"), exit(0);

   saddr.sin_family = PF_INET;
   saddr.sin_port = htons(4849); // listen on port 4096
   saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind socket to any interface
   status = bind(sock, (sockaddr *)&saddr, sizeof(sockaddr_in));

   if ( status < 0 )
     perror("Error binding socket to interface"), exit(0);

   imreq.imr_multiaddr.s_addr = inet_addr("226.0.0.1");
   imreq.imr_interface.s_addr = htonl(INADDR_ANY); // use DEFAULT interface

   // JOIN multicast group on default interface
   **status = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
              (const char *)&imreq, sizeof(ip_mreq));** // This line return -1



   socklen = sizeof(struct sockaddr_in);

   // receive packet from socket
   status = recvfrom(sock, buffer, MAXBUFSIZE, 0, 
                     (struct sockaddr *)&saddr, &socklen);


   if(status < 0)

   {
      printf("Received failed");
   }
   else

   {
       printf("received buffer : %s",buffer);
   }

   // shutdown socket
   shutdown(sock, 2);
   // close socket
   closesocket(sock);

   return 0;
}

// I think it sis not able to register the interface address. Any body has idea on this ? Thanks in advance

A: 

Try taking help from GetLastError

DWORD err = GetLastError();

In the watch window type err,hr

It shows An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call. (#define WSAENOPROTOOPT)

Then I looked at http://msdn.microsoft.com/en-us/library/ms740476(VS.85).aspx

IPPROTO_IP doest not appear in the list of supported levels.

Since I am not too familiar with this, I could help only so much. Sorry. Hopefully, you can take it up from here

Chubsdad
@chubsdad:It so nice of u .Pls dont mention sorry
Swapnil Gupta
A: 

I resolved the above issue.

Just replace ws2_32.lib with wsock32.lib. This will resolve the issue.

Swapnil Gupta