Adam
Adam

Reputation: 1744

c++ Poco libraries UDP DatagramSocket send example raise a Poco::Net::NetException

I'm new to POCO lib and I'm doing the net examples on the tutorial pdf.

When I'm running the DatagramSocket send example I'd always get a Poco::Net::NetException.

If I use the port '514' given by the tutorial, I'll get a Poco::IOException "I/O Error".

My environment is kubuntu 12.04, kernel 3.2.0-57-generic. Anyone knows about this?? Thx!!

// DatagramSocket send example
#include <Poco/Net/DatagramSocket.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Timestamp.h>
#include <Poco/DateTimeFormatter.h>

#include <string>

int main()
{
    Poco::Net::SocketAddress sa(Poco::Net::IPAddress(), 12345);
    Poco::Net::DatagramSocket dgs(sa);

    std::string syslogMsg;
    Poco::Timestamp now;
    syslogMsg = Poco::DateTimeFormatter::format(now, "<14>%w %f %H:%M:%S Hello, World!");

    dgs.sendBytes(syslogMsg.data(), syslogMsg.size());

    return 0;
}//main

Edit:

Thanks for Joachim Pileborg for suggestions on displayText() of exception. It shows this: "Net Exception: Destination address required"

And I amended the code like this and it worked:

Poco::Net::SocketAddress recver("localhost", 1234);
dgs.sendTo(syslogMsg.data(), syslogMsg.size(), recver);

But if I want to use sendBytes(), is there a way to put in a default receiver's address??

Upvotes: 2

Views: 5106

Answers (1)

WolfCoder
WolfCoder

Reputation: 137

call connect(...) function of the Poco socket class.

Poco::Net::SocketAddress recver("localhost", 1234);
dgs.connect(recver);

and from there on you can dgs.sendBytes().

Hope this helps.

Upvotes: 2

Related Questions