Socket Broadcast Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
tsaphah
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Jul 04, 2005 1:31 am

Socket Broadcast

Post by tsaphah »

I'm wanting to broadcast a packet to my subnet (ie 255.255.255.255), and I looked at the thread found here: http://forums.wxwidgets.org/viewtopic.php?t=4820
but it didn't seem like upCASE actually sent information.

I believe he was setting up a UDP socket to listen for broadcasts. Is that correct?

If that is the case how do I use wxSocketClient to broadcast? Is this as simple as calling wxSocketBase::Write() from the wxDatagramSocket instance which was setup to use AnyAddress() ?

Code from the referenced thread.

Code: Select all

        wxIPV4address addr;
        addr.AnyAddress();
        addr.Service(30301);
        m_multicastSocket = new    wxDatagramSocket(addr,wxSOCKET_REUSEADDR);

       if(m_multicastSocket->Ok())
        {        ip_mreq mreg;
                memset(&mreq,0,sizeof(ip_mreq));
                mreq.imr_multiaddr.s_addr = inet_addr(GROUP_DEFAULT);        
                mreq.imr_interface.s_addr = htons(INADDR_ANY);
                m_multicastSocket->SetOption(IPPROTO_IP, IP_ADD_MEMBERSHIP, (char far *)&mreq,sizeof(mreq));

                m_multicastSocket->SetNotify(wxSOCKET_INPUT_FLAG);
                m_multicastSocket->Notify(true);
                m_multicastSocket->SetEventHandler(*this, ID_MULTICAST);
        }
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Re: Socket Broadcast

Post by upCASE »

tsaphah wrote:If that is the case how do I use wxSocketClient to broadcast? Is this as simple as calling wxSocketBase::Write() from the wxDatagramSocket instance which was setup to use AnyAddress() ?
It's quite simple, as datagram sockets don't use real connections.

To send data to the group the wxIPV4address to send to must have set the Hostname() to the GROUP_DEFAULT you defined. The port in this case is 30301. Then simply use m_multicastSocket->Write().
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
tsaphah
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Jul 04, 2005 1:31 am

Re: Socket Broadcast

Post by tsaphah »

upCASE wrote:To send data to the group the wxIPV4address to send to must have set the Hostname() to the GROUP_DEFAULT you defined. The port in this case is 30301. Then simply use m_multicastSocket->Write().
Ok, sounds good. Thanks for the reply upCASE =)
Maddis
Knows some wx things
Knows some wx things
Posts: 36
Joined: Tue Jul 25, 2006 7:14 am

Post by Maddis »

Post to old topic.

I'm having troubles to get that broadcast thingie working. Actually I'm having troubles to get it even compiled!

So far I've this:

Code: Select all


        m_serverAddr.Hostname(GROUP_DEFAULT)
        m_serverAddr.Service(INFO_PORT);
        m_sock = new wxDatagramSocket( m_serverAddr, wxSOCKET_NOWAIT );


       // Then later

        // This doesn't work without the struct-word. 
        struct ip_mreq mreg;

        m_sock->SetEventHandler(*this, idUDPSocket);
        m_sock->SetNotify(wxSOCKET_INPUT_FLAG);
        m_sock->Notify(true);

        memset(&mreq,0,sizeof(ip_mreq));
        mreq.imr_multiaddr.s_addr = inet_addr(GROUP_DEFAULT);
        mreq.imr_interface.s_addr = htons(INADDR_ANY);
        m_sock->SetOption(IPPROTO_IP, IP_ADD_MEMBERSHIP, (char far *)&mreq,sizeof(mreq));

Rest of the errors:
- `GROUP_DEFAULT' undeclared
- aggregate `ip_mreq mreg' has incomplete type and cannot be defined
- `IP_ADD_MEMBERSHIP' undeclared

Those are the worst ones. For example, I couldn't find GROUP_DEFAULT from wxwidgets includes or from mingw. All I want is to send broadcast messages!

One another thing. If/when I get that to work and I want to send message to specific IP can I just use SendTo and later if I want to send another broadcast, do I initialize socket again or can I just use Write to send broadcast?
Post Reply