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
QtWS25 Last Chance

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 20 May 2019, 11:35 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.

    A 1 Reply Last reply 20 May 2019, 12:07
    0
    • C ClingerWinger
      20 May 2019, 11:35

      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.

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 20 May 2019, 12:07 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 20 May 2019, 13:28 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.

        A 1 Reply Last reply 20 May 2019, 14:34
        2
        • C ClingerWinger
          20 May 2019, 13:28

          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.

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 20 May 2019, 14:34 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

          4/4

          20 May 2019, 14:34

          • Login

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