[SOLVED] UDP send data question
-
I'm trying to send a UDP datagram, and I think I perhaps don't understand the QUdpSocket class as well as I should...
Code so far:in my class:
@public slots:
void socket_init()
{
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::LocalHost, 4595);connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagram())); }
private slots:
void readPendingDatagram() { while (udpSocket->hasPendingDatagrams()) { //QString dialog; in_data.resize(udpSocket->pendingDatagramSize()); / QHostAddress sender; udpSocket->readDatagram(in_data.data(), in_data.size(), &sender, &senderPort); QQuickItem* object = myDialog->rootObject(); QObject *textinput = object->findChild<QObject*>("textinput"); if (textinput) { textinput->setProperty("text", in_data); } } return; }
private:
QUdpSocket* udpSocket;
@in my main function, after the class object is created:
@
test2 *funcs = new test2(0, view);
funcs->socket_init();
@and in the function that sends the datagram I'm having trouble. I've created a sample datagram:
@QByteArray datagram = "Hi Bob!";@
but I'm not sure how to specify the receiver's address. The class documentation was a little abstract for me. If I want to take the ascii address of say "10.1.10.55" stored like so:
@QString bob_address = "10.1.10.55"@
How do I insert it into the writeDatagram() function?
@udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::bob_Address, 4595);@
What am I missing here? Any help would be appreciated. Thanks!!
-
"try read here":http://qt-project.org/doc/qt-5/qhostaddress.html#QHostAddress-6
QHostAddress(bob_address); -
Thanks MrMNight,
I did read through all that already... So you're telling me that all I have to do to set the host address is this:
@bob_address = new QHostAddress("10.1.10.55");@
then send it out like this:
@udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::bob_Address, 4595);@
Right? I could swear I've done that already, but I'll try that and report back.
-
Nope, that didn't work. I'm still getting the same error:
@'bob_address' is not a member of 'QHostAddress'@
This doesn't make any sense to me... All that data is in the constructor of my class, and is passed into the class functions. The first error shows on this line of the constructor:
@udpSocket->bind(QHostAddress::bob_address, 4595);@
I followed the "sender" and "receiver" examples posted in these threads but am still having trouble:
"receiver.cpp":http://qt-project.org/doc/qt-5/qtnetwork-broadcastreceiver-receiver-cpp.html
"receiver.h":http://qt-project.org/doc/qt-5/qtnetwork-broadcastreceiver-receiver-h.html
"sender.cpp":http://qt-project.org/doc/qt-5/qtnetwork-broadcastsender-sender-cpp.html
"sender.h":http://qt-project.org/doc/qt-5/qtnetwork-multicastsender-sender-h.htmlas well as several examples on stackoverflow.com, and "this page":http://www.bogotobogo.com/Qt/Qt5_QUdpSocket.php too.
-
You can simply wite QHostAddress::Broadcast because this is special case "described here:":http://qt-project.org/doc/qt-5/qhostaddress.html#SpecialAddress-enum , but you can't write QHostAddress::YourOwnName, you must construct object QHostAddress like this: QHostAddress("2.2.2.2") or like this: QHostAddress(someQStringThere), see documentation of "QHostAddress constructors:":http://qt-project.org/doc/qt-5/qhostaddress.html#QHostAddress
-
Ok, so my application HAS TO be specific in this case.... I can't just use Broadcast on 255.255.255.255 - it has to be a specific address - unless I'm misunderstanding what you're getting at...
So I did this:
@udpSocket->bind(QHostAddress("10.1.10.55"), 4595);@in my constructor and it compiles... but is it correct?
I also tried this:
@udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress("10.1.10.55"), 4595);@
In my sending function - I'll assume it's correct since it compiled. I'll assume I don't need to bind it in the constructor as I did above if I just declare it in the "writeDatagram" function? Going to try to run the program in a moment. Few more tweaks first. Let you know if it works...
Thanks for taking the time on this - it's a little abstract for me still...
-
Hi,
Your "not a member" error was because of this:
@QHostAddress::bob_address@
You are using colons rather than parenthesis
@QHostAddress(bob_address)@
Is the correct version
-
oh... I misread the documentation then. I assume that case applies to using a QString address. I'm now able to transmit, thank you for your help! Now to make it receive :-)
-
Not really, calling something like "MyClass::something" means either you want to get an enum or a static variable member value
You're welcome !
Happy coding !
-
Thanks SGaist! This project I'm working on is my first ever windows application. I'm usually doing a little bit of C for embedded processors, and mostly simpler stuff. But this forum has been great so far, and has done the one thing I wanted: Help me understand this stuff!!