Results 1 to 6 of 6

Thread: QDataSream: No Device

  1. #1
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QDataSream: No Device

    Hi. I'm writing a few small test socket programs using Qt libraries without using the event loop (i.e. no Signals and Slots). Here is the source code for a small server:

    #include <QtNetwork>
    #include <iostream>
    #include <qdatastream.h>

    int main(int *argc, char *argv[]){

    QTcpServer server;
    QTcpSocket *socket;
    server.listen(QHostAddress::LocalHost,8000);
    bool timeout;
    server.waitForNewConnection(50000,&timeout);
    socket=server.nextPendingConnection();
    QDataStream in(socket);
    in.setVersion(QDataStream::Qt_4_0);
    int number;
    in>>number;
    std::cout<<number<<std::endl;
    }

    and a simple client:


    #include <QtNetwork>
    #include <iostream>
    #include <qdatastream.h>

    int main(int *argc, char *argv[]){

    QTcpSocket socket;
    socket.connectToHost(QHostAddress::LocalHost,8000) ;
    if (!socket.waitForConnected(5000)) {
    std::cout<<"Cannot connect to GUI."<<std::endl;
    return 0;
    }
    QDataStream out(&socket);
    out.setVersion(QDataStream::Qt_4_0);
    out<<1;
    }

    when I run the server (receiving data) and then the client, the server displays the message "QDataStream: No device" followed by a zero. What am I doing wrong? thanks.

    Edit: I forgot to note that the client displays the error message "Cannot connect to GUI"
    Last edited by jonging; 20th June 2006 at 20:33.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataSream: No Device

    QTcpServer needs an event loop running. So does QTcpSocket.

  3. #3
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataSream: No Device

    Actually, the QT documentation site says that both QTcpSocket and QTcpServer can be used without an event loop.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataSream: No Device

    You must initialize timeout to false (see here) and you have to invoke QIODevice::waitForReadyRead() and QIODevice::waitForBytesWritten() in proper places to wait for data to be passed over the network.

  5. The following user says thank you to jacek for this useful post:

    jonging (20th June 2006)

  6. #5
    Join Date
    Jun 2006
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataSream: No Device

    Awesome, thanks a lot. I would never have been able to locate that QT bug. I still have a little trouble with the two other blocking calls because I get a segmentation fault. Do you have any idea what could be causing this?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataSream: No Device

    Most segfaults are caused by dangling pointers. Use a debugger to see where it crashes and what parameters were passed to functions on the call stack.

Similar Threads

  1. Modifying the Display on Device
    By mahe2310 in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 13th April 2006, 04:36
  2. How Qt/Embedded detects the keypad input from device?
    By mahe2310 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 4th April 2006, 07:08
  3. how to export images of Qt/Embedded on Device
    By mahe2310 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 23rd March 2006, 12:05
  4. Reading/Writing data from/to usb device
    By yellowmat in forum Newbie
    Replies: 2
    Last Post: 6th March 2006, 13:08
  5. Interaction between QT/Embedded and LCD display device
    By mahe2310 in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 22nd February 2006, 09:37

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.