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. QTcpSocket in QThread Problem In Qt5.1
QtWS25 Last Chance

QTcpSocket in QThread Problem In Qt5.1

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.5k 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.
  • A Offline
    A Offline
    alizadeh91
    wrote on last edited by
    #1

    I've created a QThread and in run method i have a QTcpSocket variable. It successfully send data to the client. But when the program exit from run function, and I create another thread with the same socket descriptor it fails to send data. The error is : "Invalid Socket Descriptor". It seems that the first QTcpSocket is not deleted correctly and it uses the port so the second thread with the same port can not open it.
    Please Advice.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      compor
      wrote on last edited by
      #2

      hello,

      on *nix platforms this error usually maps to the socket API EBADF or EALREADY errors. more on those "here":http://pubs.opengroup.org/onlinepubs/009695399/functions/connect.html

      on which specific method call do you get that error? is it QAbstractSocket::connectToHost?
      what do you mean when you say that you are re-using the same socket descriptor?
      do you perform operations on the raw socket file descriptor with e.g. QAbstractSocket::setSocketDescriptor or QAbstractSocket::socketDescriptor? why?

      if you do re-use it like that and provide the same fd for the new QTcpSocket, then the fd is bogus since it has indeed been closed when the last QTcpSocket went out of scope or when (and if) you explicitly called QAbstractSocket::disconnectFromHost.

      purposes as understood by the purposer will be misunderstood by others

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KA51O
        wrote on last edited by
        #3

        About the socket you create in the QThread: is it allocated on the heap or the stack ? Where exactly do you create it (inside the run function, or in the constructor, is it a member var of your QThread subclass) ? If it is allocated on the heap where and when do you delete it ?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alizadeh91
          wrote on last edited by
          #4

          I've tested it both with heap and stack and both run with same issue. I've defined it in run method.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on last edited by
            #5

            Ok. If you create the object inside the run method the object is living inside the context of the thread which is managed by your QThread subclass. When the run method ends the thread no longer has a running eventloop. So even if you created the socket on the heap and didn't delete it it won't be able to work since it relies on signals and slots.

            First of I would suggest that you delete the socket when the QThread is done (i.e. its run method ends, no working eventloop). This is of course only neccessary if you created it on the heap. And then create a new socket in the next thread. No need to use socket descriptors for this.

            I'm not sure if its possible to reuse sockets in different threads. You could try to create them in the main thread and then use moveToThread() to shift them from thread to thread. But for this you would have to use the "worker object approach":http://qt-project.org/wiki/QThreads_general_usage instead of the subclassing QThread approach.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              alizadeh91
              wrote on last edited by
              #6

              Special thanks :)
              I'll try that.

              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