Results 1 to 2 of 2

Thread: Sending and Receiving datagrams in one application

  1. #1
    Join Date
    Jan 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Sending and Receiving datagrams in one application

    Hi,
    This should be a simple problem for those QT experts out there. I'm trying to send and receive UDP datagrams using a single QT application (don't ask why). I thought this would work just like it would with two applications, which have worked for me in the past. I have two threads running. The first sends out the message and the second thread reads in the message. The application successfully sends the message out on the network b/c I've used the QT broadcast receiver application to see if it can read the message and it does. But when I put this same code into the same application as the sender it doesn't work. I get no errors when i compile nor run the program...just nothing is received. I've inserted a qDebug() statement that tells me when a datagram is ready and I never see that text. So, I'm assuming that the message is never received. Now, I did change the port number between the sending and receiving code segments b/c I get error messages with binding to the same port. It is the same application so it perhaps makes sense that I can't bind to the same port, but with implementing the same thing with two applications (the QT examples do this as well), I use the same port number for both sending and receiving. I'm thinking there is a simple solution here b/c I'm sending and receiving on the same machine and same application. Anyway, thanks in advance for any help.

    1st thread code segment:
    QByteArray message;
    message.append(msg);
    //qDebug() << message;

    QByteArray datagram = message;
    udpSocket->writeDatagram(datagram.data(), datagram.size(),
    QHostAddress::Broadcast, 45454); //I've tried using "localhost" here as well

    2nd thread code segment:
    receiving_network::receiving_network()
    {

    rcvSocket = new QUdpSocket(this);

    rcvSocket->bind(45453, QUdpSocket::ShareAddress);

    connect(rcvSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
    }

    void receiving_network:rocessPendingDatagrams(){

    qDebug() << "Processing datagram";
    while (rcvSocket->hasPendingDatagrams()) {
    QByteArray datagram;
    datagram.resize(rcvSocket->pendingDatagramSize());
    rcvSocket->readDatagram(datagram.data(), datagram.size());
    received_data = datagram.data();
    }
    }

  2. #2
    Join Date
    Jan 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sending and Receiving datagrams in one application

    I figured it out. Well, first I must be on the same port or I won't get the messages (duh). Next, I must use the same Localhost address. The following code works for sending and receiving messages from a single application:

    sending thread:
    udpSocket = new QUdpSocket(this);
    udpSocket->writeDatagram(datagram.data(), datagram.size(),
    QHostAddress::LocalHost, 45454);

    receiving thread:
    rcvSocket = new QUdpSocket(this);
    rcvSocket->bind(QHostAddress::LocalHost,45454);
    datagram.resize(rcvSocket->pendingDatagramSize());
    rcvSocket->readDatagram(datagram.data(), datagram.size());
    received_data = datagram.data(); //received_data is just a QString to store the datagram

Similar Threads

  1. Sending and receiving SMS messages with a GSM modem
    By adamb924 in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2013, 07:51
  2. Receiving application foreground event
    By kokilakr in forum Qt Programming
    Replies: 0
    Last Post: 24th June 2010, 11:31
  3. Replies: 3
    Last Post: 8th April 2010, 07:59
  4. QUdpSocket: sending and receiving on the same socket?
    By mhoover in forum Qt Programming
    Replies: 16
    Last Post: 17th June 2009, 03:28
  5. Sending large datagrams(very large)
    By marcel in forum General Programming
    Replies: 1
    Last Post: 16th February 2008, 21:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.