Results 1 to 7 of 7

Thread: question about "fortune server"

  1. #1
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default question about "fortune server"

    Hi, I'm stumped as to of why this code causes my application to crash when I telnet to it. It's almost directly copied from the fortune example here: http://doc.trolltech.com/4.1/network-fortuneserver.html

    Qt Code:
    1. void send() {
    2. QByteArray block;
    3. QDataStream out(&block, QIODevice::WriteOnly);
    4. out.setVersion(QDataStream::Qt_4_0);
    5. out << (quint16)0;
    6. out << "here's some text i'm sending...";
    7. out.device()->seek(0);
    8. out << (quint16)(block.size() - sizeof(quint16));
    9.  
    10. QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    11. connect(clientConnection, SIGNAL(disconnected()),
    12. clientConnection, SLOT(deleteLater()));
    13.  
    14. clientConnection->write(block);
    15. clientConnection->disconnectFromHost();
    16. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Dumbledore; 13th October 2007 at 19:25.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: question about "fortune server"

    Probably because clientConnection is NULL. You don't test for that, yet it is possible for it to be.

    And about the byte array: you're writing something to ot, next you're positioning the file pointer to the start of the stream and write something again. The first data gets lost.

  3. #3
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: question about "fortune server"

    Yeah, like I said it's from an example so I'm trying to learn network programming with QT.

    I'm just trying to not crash at this point. This still crashes though:

    Qt Code:
    1. QByteArray block;
    2. QDataStream out(&block, QIODevice::WriteOnly);
    3. out.setVersion(QDataStream::Qt_4_0);
    4. out << "here's some text i'm sending...";
    5.  
    6.  
    7. QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    8. if ( NULL != clientConnection ) {
    9. connect(clientConnection, SIGNAL(disconnected()),
    10. clientConnection, SLOT(deleteLater()));
    11.  
    12. clientConnection->write(block);
    13. clientConnection->disconnectFromHost();
    14. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: question about "fortune server"

    Could we see the backtrace from debugger?
    J-P Nurmi

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: question about "fortune server"

    Can't you debug it, to see exactly at what line it crashes?

  6. #6
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: question about "fortune server"

    I've never used a debugger before. In other words, the code looks fine and something else is going on?

    EDIT: Alright I discovered it's because the instance of tcpServer it was using had never been allocated yet. It was a wild pointer. Thanks for the help, normally I don't ask such stupid questions but I'm completely new to this stuff.
    Last edited by Dumbledore; 13th October 2007 at 20:32.

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: question about "fortune server"

    I've never used a debugger before. In other words, the code looks fine and something else is going on?
    Code looks good all the time...
    You should start using a debugger. These days you can't do any serious programming without one.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.