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. Crash in QTcpSocket inherited class : Help Needed
Forum Updated to NodeBB v4.3 + New Features

Crash in QTcpSocket inherited class : Help Needed

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 5.0k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    sergico
    wrote on last edited by
    #1

    Hi All,

    I created a class that inhertits from QTcpSocket, because I need a QTcpSocket that binds on a local address (AFAIK such a feature is not supported by the standard QTcpSocket)
    The skeleton of my class is:

    @
    class QTcpBoundSocket : public QTcpSocket
    {
    Q_OBJECT

    /* some class stuff here */
    

    public:
    QTcpBoundSocket(QObject *parent = 0);
    virtual ~QTcpBoundSocket() {}

    virtual void connectToHost(const QString& aRemoteHostname, quint16 aRemotePort,
                               const QString& aLocalHostname, OpenMode openMode = ReadWrite);
    /* other stuff here */
    

    };
    @

    The class itself seems to work but I got a crash each time the class destructor is called.
    Is there anything clearly wrong in the way I'm inheriting from the QTcpSocket class?

    Here's the sample code that cause the crash:

    @
    QTcpBoundSocket* myBoundSocketPtr = new QTcpBoundSocket();
    Q_ASSERT(myBoundSocketPtr);

    QObject::connect ( myBoundSocketPtr, SIGNAL(disconnected()),
                       myBoundSocketPtr, SLOT(deleteLater()) );
    
    bool b = false;
    QByteArray data;
    
    qDebug() << "Connecting to " << aRemoteHost << ":" << aRemotePort << "from" << aLocalHost;
    myBoundSocketPtr->connectToHost(aRemoteHost, aRemotePort, aLocalHost);
    
    b = myBoundSocketPtr->waitForConnected();
    if (b)
    {
        qDebug() << "Bound socket connected :)";
        myBoundSocketPtr->write(QString("GET index.html\r\n").toAscii());
        myBoundSocketPtr->waitForReadyRead();
        while( (data = myBoundSocketPtr->readAll()).length() )
        {
            qDebug() << data;
        }
    }
    myBoundSocketPtr->close();
    

    @

    Thanks
    Sergio

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Welcome to devnet

      What do you mean with binds on a local address?

      The question is probably how your destructor is invoked?
      The problem may be deleteLater() when you destruct a connected socket.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sergico
        wrote on last edited by
        #3

        Hi Koahnig,

        I'll try to answer your questions:

        My connectToHost(...) method uses the POSIX functions to create a tcp socket and bind it to the local IP address I specify as a parameter. Then I use the setSocketDescriptor() method to pass the control of the socket to the (base class) QTcpSocket object.

        I connected the disconnect() signal to the deleteLater(), because I thought I need to delete the object only after the connection has been closed. Some other Qt forum posts suggests this. Is that wrong?
        My class do not allocate (explicitly) any memory, so the destructor basically does nothing.

        Thanks
        Sergio

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          Could it be that you are trying to set up a pipe?
          You want to connect an external IP address to an internal IP address, but you are not interested to do anything other with that information in your application?
          I do not see the point to use Qt and POSIX functions in parallel for establishing sockets. I would rather stay with either method. But this qualifies more like a personal preference.

          QTcpSocket may be connected to local address (127.0.0.1) as well.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sergico
            wrote on last edited by
            #5

            Hi,

            I see your point in saying that I should only use POSIX or Qt rather than mixing them, but I'm designing this class to integrate it in a bigger project that is Qt based, that's basically the rationale.
            When I talk about binding to a local address I do not mean localhost (127.0.0.1) but an address assigned to a local interface. The TCP connection will be done then to a remote address. (but from the bound address)
            On my system I have multiple ifaces with different addresses (let's say 192.168.0.1 and 10.0.0.1 for instance) Is there a way to bind a socket to a local iface address with the standard QTcpSocket? I did not find how to.

            Thanks
            Sergio

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              I am doing Qt based applications, but I am not using Qt for all things inside my application. Basically I am Qt, stl and boost in one application. For instance strings, maps, lists and other things I am using from stl, because I am used to. And I have no intension to change to Qt.

              [quote author="sergico" date="1327770596"]The TCP connection will be done then to a remote address. (but from the bound address)
              On my system I have multiple ifaces with different addresses (let's say 192.168.0.1 and 10.0.0.1 for instance) Is there a way to bind a socket to a local iface address with the standard QTcpSocket? I did not find how to.[/quote]
              I cannot help you on this issue.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rich
                wrote on last edited by
                #7

                This is possible in Qt 5.0 using http://doc.trolltech.com/5.0-snapshot/qabstractsocket.html#bind but I don't know if the same functionality is offered in 4.x.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sergico
                  wrote on last edited by
                  #8

                  Hi Rich,

                  Thanks for the tip. I was not aware Trolltech introduced this feature in Qt 5.x At the moment we are using Qt 4.7. I was not able to find a similar functionality in this version.

                  Thanks
                  Sergio

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rich
                    wrote on last edited by
                    #9

                    Well, since most of the surrounding socket code is the same in Qt 5.0, you could have a look at the implementation from there. It might help you out.

                    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