A
A
Artem2012-09-25 13:40:59
Computer networks
Artem, 2012-09-25 13:40:59

How to make broadcast message work?

I use UDP broadcast in my program (MS VC++). I'm testing between a desktop computer (Win7) and a netbook (XP). They are networked via Wi-Fi: the netbook has a built-in, and a whistle is plugged into the computer, which works in access point mode. The problem is that if you send a message from a computer to a netbook, then it does not reach. The reverse works. And if you combine devices into a network with a twisted pair cable through a router, it also works in both directions. Apparently, the problem is precisely in such a connection through a whistle. Whether it is possible to bypass it somehow?

Here is the send code:

void SendBroadcast(char * message) {
    SOCKET s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(s == -1) {
        printf("Error in creating socket");
        return;
    }
    char opt = 1;
    setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char));
    SOCKADDR_IN brdcastaddr;
    memset(&brdcastaddr,0, sizeof(brdcastaddr));
    brdcastaddr.sin_family = AF_INET;
    brdcastaddr.sin_port = htons(MY_PORT);
  brdcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
    int len = sizeof(brdcastaddr);

    char sbuf[1024];
  //Заполнение sbuf здесь

    sendto(s, sbuf, (int)strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
    closesocket(s);
}


And this is the receive code:
DWORD WINAPI BroadcastProcess(LPVOID arg) {
  SOCKET broadcastServerSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if(broadcastServerSock == -1) {
    printf("Error in creating socket");
    return 0;
  }

  SOCKADDR_IN UDPserveraddr;
  memset(&UDPserveraddr,0, sizeof(UDPserveraddr));
  UDPserveraddr.sin_family = AF_INET;
  UDPserveraddr.sin_port = htons(MY_PORT);
  UDPserveraddr.sin_addr.s_addr = INADDR_ANY;

  int len = sizeof(UDPserveraddr);

  if(bind(broadcastServerSock, (SOCKADDR*)&UDPserveraddr,sizeof(SOCKADDR_IN)) < 0) {
    printf("ERROR binding in the server socket");
    return 0;
  }


  char rbuf[1024];
  SOCKADDR_IN clientaddr;
  len = sizeof(clientaddr);
  while(true) {
    int received=recvfrom(broadcastServerSock,rbuf, 1023, 0, (sockaddr*)&clientaddr, &len);
    if(received>0) {
      rbuf[received]='\0';

      char *p = inet_ntoa(clientaddr.sin_addr);
      int serverportno = ntohs(clientaddr.sin_port);

      MessageBoxA(g_hWnd,rbuf,"",MB_OK);
    }
  }
  return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JDima, 2012-09-25
@bartwell

Do you realize that your chakras will be seriously bent from writing code that relies on broadcasts, and after that you will never be able to bulge them out?
Use L2 or L3 multicast. On unmanaged switches, it behaves like a broadcast. On managed everything is much nicer.

Q
Qwadrat, 2012-09-25
@Qwadrat

Perhaps in the case of sending from a computer, the broadcast packets come from the wrong interface?
Use Wireshark to diagnose.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question