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 disconnects after data writing

QUdpSocket disconnects after data writing

Scheduled Pinned Locked Moved Solved General and Desktop
networkqudpsocket
4 Posts 2 Posters 1.2k 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.
  • C Offline
    C Offline
    ClingerWinger
    wrote on last edited by ClingerWinger
    #1

    Hello everyone! I'm trying to write a client for server (which is not mine) using QUdpSocket. But I have some problems. The main one is that socket goes to unconnected state whenever after any data was written to it.

    Init socket example:

    mUdpSocket = new QUdpSocket(this)
    
    connect(mUdpSocket, &QUdpSocket::readyRead, this, &SessionManager::onSocketReadyRead);
    connect(mUdpSocket, &QUdpSocket::bytesWritten, [](quint64 b) { qDebug() << "____ UDP BYTES WRITTEN:" << b; });
    connect(mUdpSocket, &QUdpSocket::stateChanged, [this](QAbstractSocket::SocketState socketState) {  qDebug() << "____ UDP SOCKET STATE CHANGED:" << socketState; });
    
    qDebug() << "____ BIND" << mUdpSocket->bind(QHostAddress::Any);
    

    Write example:

    void SessionManager::sendMessage(const QByteArray& message)
    {
        if (!message.isEmpty())
        {
            mUdpSocket->writeDatagram(message, QHostAddress(mHost), G_SESSION_PORT);
            // TODO: check written != -1
        }
    }
    

    Problem occurs after calling writeDatagram. I can see that 76 bytes was written succesfully and was sent to server: signal bytesWritten reports the same amount of bytes written as in sendMessage method. I also can see with Wireshark that package was sent successfully to the server. After that socket goes to unconnected state with no error (QAbstractSocket::UnknownSocketError). At that moment I can see that server tries to send me several packages, but no readyRead signal was emmited from socket. Can someone help me? I dont know what is going wrong.

    aha_1980A 1 Reply Last reply
    0
    • C ClingerWinger

      Hello everyone! I'm trying to write a client for server (which is not mine) using QUdpSocket. But I have some problems. The main one is that socket goes to unconnected state whenever after any data was written to it.

      Init socket example:

      mUdpSocket = new QUdpSocket(this)
      
      connect(mUdpSocket, &QUdpSocket::readyRead, this, &SessionManager::onSocketReadyRead);
      connect(mUdpSocket, &QUdpSocket::bytesWritten, [](quint64 b) { qDebug() << "____ UDP BYTES WRITTEN:" << b; });
      connect(mUdpSocket, &QUdpSocket::stateChanged, [this](QAbstractSocket::SocketState socketState) {  qDebug() << "____ UDP SOCKET STATE CHANGED:" << socketState; });
      
      qDebug() << "____ BIND" << mUdpSocket->bind(QHostAddress::Any);
      

      Write example:

      void SessionManager::sendMessage(const QByteArray& message)
      {
          if (!message.isEmpty())
          {
              mUdpSocket->writeDatagram(message, QHostAddress(mHost), G_SESSION_PORT);
              // TODO: check written != -1
          }
      }
      

      Problem occurs after calling writeDatagram. I can see that 76 bytes was written succesfully and was sent to server: signal bytesWritten reports the same amount of bytes written as in sendMessage method. I also can see with Wireshark that package was sent successfully to the server. After that socket goes to unconnected state with no error (QAbstractSocket::UnknownSocketError). At that moment I can see that server tries to send me several packages, but no readyRead signal was emmited from socket. Can someone help me? I dont know what is going wrong.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by aha_1980
      #2

      @ClingerWinger the most likely problem is, that your QUdpSocket is destroyed - directly or indirectly - after writeDatagram().

      Its impossible to guess more from your current code, so if my answer doesnt help you you should upload the complete example so others can try it.

      Regardsü

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • C Offline
        C Offline
        ClingerWinger
        wrote on last edited by
        #3

        Just realised that I forgot what happened at my main.cpp. Object SessionManager was created at lambda, and destroyed there immediately. Sorry for you bother.

        aha_1980A 1 Reply Last reply
        2
        • C ClingerWinger

          Just realised that I forgot what happened at my main.cpp. Object SessionManager was created at lambda, and destroyed there immediately. Sorry for you bother.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ClingerWinger Glad you figured it out and thanks for the feedback :)

          Qt has to stay free or it will die.

          1 Reply Last reply
          0

          • Login

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