CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: broadcasting

  1. #1
    Join Date
    Jan 2005
    Posts
    63

    broadcasting

    Hi,
    I was wondering how to broadcast packets onto a private network using winsock. I have used winsock before to set up a server/client connection but I do not understand how to have 1 machine broadcast it's data to all other machines at one go. Thanks
    Amish

  2. #2
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    Re: broadcasting

    Quote Originally Posted by axr0284
    I do not understand how to have 1 machine broadcast it's data to all other machines at one go
    You have to broadcast ur data on 255.255.255.255 IP.
    A special type of IP address is the limited broadcast address 255.255.255.255. A broadcast involves delivering a message from one sender to many recipients. Senders direct an IP broadcast to 255.255.255.255 to indicate all other nodes on the local network (LAN) should pick up that message. This broadcast is 'limited' in that it does not reach every node on the Internet, only nodes on the LAN
    Following is the sample code for broadcasting.
    Code:
    char               SBuf[512];
    int                  SBufLength = 512;
    
    SOCKET               SenderSock;
    SOCKADDR_IN     RemoteAddr;
    
    SenderSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    
    RemoteAddr.sin_family = AF_INET;
    RemoteAddr.sin_port = htons(5000);    
    RemoteAddr.sin_addr.s_addr = inet_addr(INADDR_BROADCAST);	//INADDR_BROADCAST = "255.255.255.255"
    
    
    sendto(SenderSock, SBuf, SBufLength, 0, 
              (SOCKADDR *)&RemoteAddr, sizeof(RemoteAddr));
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: broadcasting

    It's my understanding that both the sender socket and the recipient sockets must have SO_BROADCAST set with setsockopt, before any broadcasts take place.

    Also, I think the sender must call connect(), even though connect() is ordinarily not needed for UDP, but I'm not certain of this either.

    Mike

  4. #4
    Join Date
    Jan 2005
    Posts
    63

    Re: broadcasting

    What if I want multiple programs on the same computer to communicate via localhost ip address. Is that possible using UDP or TCP.
    Amish

  5. #5
    Join Date
    Aug 2001
    Posts
    507

    Re: broadcasting

    YEs Amish, u can have a many programs communicate with each other on the local host as u want, u can do that on UDP and TCP both ... which ever suites ur fancy ...

    But i dont think that is a good technique, Can u tell us, what u want to do then we can tell u a good way of doing it ....
    If you found my reply to be useful, please dont hesitate to rate it.


    DO NOT kick the Axe if it doesnt fall on your foot.

    Salman

  6. #6
    Join Date
    Jan 2005
    Posts
    63

    Re: broadcasting

    Well I created an MANET emulator for a thesis. But it requires multiple computers with 1 real wireless transceiver connected to each computer. Since students might have to use this software, I would like to have the software be able to communicate with multiple versions of itself using tcp/ip. Then the students can test their program on one computer at home by themselves. When they are satisfied it works, they can port their programs to the lab and test it on real hardware devices. Makes sense?
    Amish

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured