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:58 AM

All replies

  • Hello Liuming.
    It is me again....

    I'm not sure, but I think that Vista ignore broadcast packets for security issues. So, I prefer that you use Multicast instead if this fullfil your requirements.
    See this link:
    http://www.sockets.com/ch16.htm#Multicast

    Monday, October 9, 2006 11:44 AM
  • I do not know if this is related, but in my experience, CAsyncSocket was completely broken until Vista RC1. See the "CAsyncSocket does not work on Vista" thread.
    Monday, October 9, 2006 2:01 PM