Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QUdpSocket Echo Server : Client Recv Problem

QUdpSocket Echo Server : Client Recv Problem

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    isml
    wrote on 19 Mar 2012, 14:31 last edited by
    #1

    I am writing a simple udp echo server, but have problem about how to recv data in udp client.
    Below is the UDP Server main logic:

    @
    m_oServerSocket = new QUdpSocket(this);
    m_oServerSocket->connect(m_oServerSocket, SIGNAL(readyRead()), this, SLOT(__onServerRecv()));
    m_oServerSocket->bind(QHostAddress::Any, sPort.toInt());
    @

    and in "__onServerRecv()":

    @
    while(m_oServerSocket->hasPendingDatagrams())
    {

    QByteArray datagram;
    datagram.resize(m_oServerSocket->pendingDatagramSize());

    QHostAddress sender;
    quint16 senderPort;
    m_oServerSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

    QDateTime dt = QDateTime::currentDateTime();
    QString sDtString = dt.toString("yyyy-MM-dd HH:mm:ss");
    QString sLog = ui->txtServerLog->toPlainText();
    sLog = sDtString + " : " + QString::fromUtf8(datagram) + "\n" + sLog;
    ui->txtServerLog->setPlainText(sLog);

    m_oServerSocket->writeDatagram(datagram, sender, senderPort);
    }
    @

    I think the Server's Code is OK because I write a simple python client and it works perfectly.But when I write my QUdpSocket Client:

    @
    m_oClientSocket = new QUdpSocket(this);
    m_oClientSocket ->connect(m_oClientSocket , SIGNAL(readyRead()), this, SLOT(__onClientRecv()));

    QString sSendMsg = ui->txtClientSend->text();
    m_oClientSocket->writeDatagram(sSendMsg.toUtf8(), QHostAddress::LocalHost, sPort.toInt());
    @

    the slot "__onClientRecv" is never called, and it means that the client's signal "readyRead" is never triggerred.

    How should I do ?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VanDerSam
      wrote on 19 Mar 2012, 18:24 last edited by
      #2

      Try something like m_oServerSocket->bind(QHostAddress::Any, sPort.toInt()); for m_oClientSocket.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        favoritas37
        wrote on 19 Mar 2012, 22:41 last edited by
        #3

        You need to call "QAbstractSocket::connectToHost":http://qt-project.org/doc/qt-4.8/qabstractsocket.html#connectToHost function at your client application and you will be fine.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          isml
          wrote on 20 Mar 2012, 02:15 last edited by
          #4

          [quote author="VanDerSam" date="1332181472"]Try something like m_oServerSocket->bind(QHostAddress::Any, sPort.toInt()); for m_oClientSocket.[/quote]

          if I use bind in client, I think the client would became server too. And it will open two udp listen port.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            isml
            wrote on 20 Mar 2012, 02:27 last edited by
            #5

            [quote author="favoritas37" date="1332196902"]You need to call "QAbstractSocket::connectToHost":http://qt-project.org/doc/qt-4.8/qabstractsocket.html#connectToHost function at your client application and you will be fine.[/quote]

            It's OK.
            But I think the Qt's Socket Api is much different from typical C socket api. I mean there is no api corresponding to connectToHost in C socket.

            1 Reply Last reply
            0

            4/5

            20 Mar 2012, 02:15

            • Login

            • Login or register to search.
            4 out of 5
            • First post
              4/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved