Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. UDP broadcast!!

UDP broadcast!!

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rickard Andersson20
    wrote on last edited by
    #1

    I found some code in my ebook: etwork programming fro Windows, MS Press. SOCKET s; BOOL bBroadcast; char *sMsg = "This is a test"; SOCKADDR_IN bcast; s = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED); bBroadcast = TRUE; setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&bBroadcast, sizeof(BOOL)); bcast.sin_family = AF_INET; bcast.sin_addr.s_addr = inet_addr(INADDR_BROADCAST); bcast.sin_port = htons(5150); sendto(s, sMsg, strlen(sMsg), 0, (SOCKADDR *)&bcast, sizeof(bcast)); Well, this is how to send... but how to receive UDB packets and then get the IP of the sender!? I think recvfrom(...) is to be used... hm.. how to get the IP!?!?!? Rickard Andersson@Suza Computing ICQ#: 50302279 (Add me!) E-mail: nikado@pc.nu I'm from the winter country SWEDEN!

    S E M 3 Replies Last reply
    0
    • R Rickard Andersson20

      I found some code in my ebook: etwork programming fro Windows, MS Press. SOCKET s; BOOL bBroadcast; char *sMsg = "This is a test"; SOCKADDR_IN bcast; s = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED); bBroadcast = TRUE; setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&bBroadcast, sizeof(BOOL)); bcast.sin_family = AF_INET; bcast.sin_addr.s_addr = inet_addr(INADDR_BROADCAST); bcast.sin_port = htons(5150); sendto(s, sMsg, strlen(sMsg), 0, (SOCKADDR *)&bcast, sizeof(bcast)); Well, this is how to send... but how to receive UDB packets and then get the IP of the sender!? I think recvfrom(...) is to be used... hm.. how to get the IP!?!?!? Rickard Andersson@Suza Computing ICQ#: 50302279 (Add me!) E-mail: nikado@pc.nu I'm from the winter country SWEDEN!

      S Offline
      S Offline
      Srini Kella
      wrote on last edited by
      #2

      I too tried the same thing some time back. I did not get help from anywhere. As my project deadline was nearing, I found a 'rude' way. I appended the IP address of the broadcaster to the broadcast message at the very end!:(

      1 Reply Last reply
      0
      • R Rickard Andersson20

        I found some code in my ebook: etwork programming fro Windows, MS Press. SOCKET s; BOOL bBroadcast; char *sMsg = "This is a test"; SOCKADDR_IN bcast; s = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED); bBroadcast = TRUE; setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&bBroadcast, sizeof(BOOL)); bcast.sin_family = AF_INET; bcast.sin_addr.s_addr = inet_addr(INADDR_BROADCAST); bcast.sin_port = htons(5150); sendto(s, sMsg, strlen(sMsg), 0, (SOCKADDR *)&bcast, sizeof(bcast)); Well, this is how to send... but how to receive UDB packets and then get the IP of the sender!? I think recvfrom(...) is to be used... hm.. how to get the IP!?!?!? Rickard Andersson@Suza Computing ICQ#: 50302279 (Add me!) E-mail: nikado@pc.nu I'm from the winter country SWEDEN!

        E Offline
        E Offline
        Ernest Laurentin
        wrote on last edited by
        #3

        You may want to try this, I never tried it with UDP socket but think it should work. BTW let me know if you try it and if it works!

        DWORD ReadSocket(SOCKET s, LPBYTE lpBuffer, DWORD dwMaxSize,
        DWORD dwTimeout /*=INFINITE*/, SOCKADDR_IN* pAddrIn /*=NULL*/)
        {
        fd_set fdRead = { 0 };
        TIMEVAL stTime;
        TIMEVAL *pstTime = NULL;

        if ( INFINITE != dwTimeout ) {
        stTime.tv_sec = 0;
        stTime.tv_usec = dwTimeout*1000;
        pstTime = &stTime;
        }

        // Set Descriptor
        if ( !FD_ISSET( s, &fdRead ) )
        FD_SET( s, &fdRead );

        // Select function returns if data is available
        DWORD dwBytesRead = 0L;
        int res = select( s+1, &fdRead, NULL, NULL, pstTime );
        if ( res > 0)
        {
        res = recvfrom( s, (LPSTR)lpBuffer, dwMaxSize, 0, pAddrIn, sizeof(SOCKADDR_IN));
        dwBytesRead = (DWORD)((res > 0)?(res) : (-1));
        }
        return dwBytesRead;
        }

        "One good thing about getting older, you don't loose the ages you've been!"

        1 Reply Last reply
        0
        • R Rickard Andersson20

          I found some code in my ebook: etwork programming fro Windows, MS Press. SOCKET s; BOOL bBroadcast; char *sMsg = "This is a test"; SOCKADDR_IN bcast; s = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED); bBroadcast = TRUE; setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&bBroadcast, sizeof(BOOL)); bcast.sin_family = AF_INET; bcast.sin_addr.s_addr = inet_addr(INADDR_BROADCAST); bcast.sin_port = htons(5150); sendto(s, sMsg, strlen(sMsg), 0, (SOCKADDR *)&bcast, sizeof(bcast)); Well, this is how to send... but how to receive UDB packets and then get the IP of the sender!? I think recvfrom(...) is to be used... hm.. how to get the IP!?!?!? Rickard Andersson@Suza Computing ICQ#: 50302279 (Add me!) E-mail: nikado@pc.nu I'm from the winter country SWEDEN!

          M Offline
          M Offline
          Mike Nordell
          wrote on last edited by
          #4

          It's in the documentation for recvfrom. The second last parameter can point to something you can get the senders IP from.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups