locked
CAsyncSocket broadcast problem in Vista RRS feed

  • Question

  • Hello Everyone:

              I have wrote some code using MFC CAsyncSocket to broadcast. The program run correctly under Windows XP, but not Vista(). The broadcast packets cannot be captured when running in Vista.
              Is there some MFC supporting problem for Vista? Or other reasons?
              I need your HELP! 
              Thanks a lot.

    The code is like this:


     int flag=1;
     int rflag = 0;
     int len = 0;

    CAsyncSocket *s;


     if( !AfxSocketInit() )
      return 1;

     if( ( s = new CAsyncSocket ) == NULL )
     {
      return 2;
     }
     struct in_addr in;
     char temp[20];
     in.S_un.S_addr = inet_addr("127.0.0.1");
     strcpy(temp, inet_ntoa(in));


     if ( !s->Create(SRC_PORT, SOCK_DGRAM, FD_READ, temp) )
     {
      int i = GetLastError();
      delete s;
      return 3;
     }

     if ( !s->SetSockOpt(SO_BROADCAST, &flag, sizeof(int), SOL_SOCKET) )
     {
      delete s;
      return 4;
     }

     len = sizeof(rflag);

     if ( !s->GetSockOpt(SO_BROADCAST, &rflag, &len, SOL_SOCKET) && rflag == 0)
     {
      delete s;
      return 5;
     }


        char          *sendbuf = "It's a UDP socket test!";
        SOCKADDR_IN    recipient;
        recipient.sin_family = AF_INET;
        recipient.sin_port = htons(1098);
        recipient.sin_addr.s_addr = inet_addr("192.168.1.3");

        s = socket(AF_INET, SOCK_DGRAM, 0);
        for(i = 0; i < 5; i++)
        {
                ret = sendto(s,sendbuf,30,0,(struct sockaddr *)&recipient,sizeof(recipient));
                if (ret == SOCKET_ERROR)
                {
                    printf("send() failed: %d\n", WSAGetLastError());
                    break;
                }
     }

    Monday, October 9, 2006 9:57 AM