Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    QUdpSocket Echo Server : Client Recv Problem

    General and Desktop
    3
    5
    6268
    Loading More Posts
    • 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
      isml last edited by

      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 Reply Quote 0
      • V
        VanDerSam last edited by

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

        1 Reply Last reply Reply Quote 0
        • F
          favoritas37 last edited by

          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 Reply Quote 0
          • I
            isml last edited by

            [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 Reply Quote 0
            • I
              isml last edited by

              [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 Reply Quote 0
              • First post
                Last post