Page 1 of 1

Socket Broadcast

Posted: Thu Nov 10, 2005 11:55 pm
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);
        }

Re: Socket Broadcast

Posted: Fri Nov 11, 2005 8:32 am
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().

Re: Socket Broadcast

Posted: Sun Nov 13, 2005 11:00 pm
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 =)

Posted: Tue Aug 01, 2006 1:01 pm
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?