0

I have these two poco programs, one sends the other receives. The receive doesn't crash, but I can't get the send to work. Sadly, the exception is not very helpful either, just saying "Net Exception". It might have to do with running both programs on the same machine, but still, the reuse address is set to true and so is the loopback.

//Receive
#include <string>
#include <iostream>

// MulticastSocket receive example
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/MulticastSocket.h>

using namespace std;

int main(int argc, char* argv[])
{
    Poco::Net::initializeNetwork();

    std::cout << "1" << std::endl << std::flush;

    //Poco::Net::SocketAddress localAddr(Poco::Net::IPAddress(), 1900);
    Poco::Net::SocketAddress localAddr("239.255.255.250", 1900);

    std::cout << "2" << std::endl << std::flush;

    Poco::Net::MulticastSocket socket(localAddr, true);

    std::cout << "3" << std::endl << std::flush;

    socket.setLoopback(true);

    std::cout << "4" << std::endl << std::flush;

    socket.setTimeToLive(4);

    std::cout << "5" << std::endl << std::flush;

    Poco::Net::SocketAddress groupAddr("239.255.255.250", 1900);

    std::cout << "6" << std::endl << std::flush;

    socket.joinGroup(groupAddr.host());

    std::cout << "7" << std::endl << std::flush;

    Poco::Net::SocketAddress sender;
    char buffer[512];
    int n = socket.receiveFrom(buffer, sizeof(buffer), sender);

    std::cout << buffer << std::endl << std::flush;

    Poco::Net::uninitializeNetwork();

    return 0;
}

//Send

#include <string>
#include <iostream>

#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/MulticastSocket.h>
#include <Poco/Net/NetException.h>

using namespace std;
using namespace Poco;

int main(int argc, char* argv[])
{
    Poco::Net::initializeNetwork();

    try
    {
        Poco::Net::SocketAddress address("239.255.255.250", 1900);
        //Poco::Net::SocketAddress address(Poco::Net::IPAddress(), 1900);

        std::cout << "1" << std::endl << std::flush;

        Poco::Net::MulticastSocket socket(address, true);

        std::cout << "2" << std::endl << std::flush;

        socket.setLoopback(true);

        std::cout << "3" << std::endl << std::flush;

        //Poco::Net::SocketAddress sender;
        char buffer[512];
        buffer[0] = 'H';
        buffer[1] = '\0';

        std::cout << "Sending " << buffer << std::endl;

        //socket.sendTo(buffer, 512, sender);
        socket.sendBytes(buffer, 512, 0);

        std::cout << "4" << std::endl << std::flush;
    }
    catch(const Poco::Net::NetException& ex)
    {
        std::cout << ex.what() << std::endl << std::flush;
    }

    Poco::Net::uninitializeNetwork();

    return 0;
}
1
  • [idf@localhost Debug]$ netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.31.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 239.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 eth0 0.0.0.0 192.168.31.2 0.0.0.0 UG 0 0 0 eth0 [idf@localhost Debug]$ ls
    – Ivan
    May 21, 2014 at 17:55

1 Answer 1

0

You have to bind to the IP of your computer and not the IP of the multicast group. You have commented the correct line out:

Poco::Net::SocketAddress localAddr(Poco::Net::IPAddress(), 1900);

Try looking at the MulticastEchoServer example that comes with Poco. It is under Net/testsuite/src/MulticastEchoServer.cpp in the sources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.