Results 1 to 9 of 9

Thread: problem with QTcpSocket

  1. #1
    Join Date
    Jan 2007
    Posts
    7
    Thanks
    2
    Platforms
    Unix/X11 Windows

    Unhappy problem with QTcpSocket

    Hi,
    I've a problem with using the QTcpSocket.
    I ty to write a programm to interact with a database over tcp.
    I have to use this way because the database is not normaly supported by Qt (it's TinySQL database).

    Here are my code so far:
    Qt Code:
    1. void connectToServer(void){
    2. pushButton_abfragen->setText(tr("Abfragen 2......"));
    3. tcpsocket.connectToHost(lineEdit_ipgypsy->text(),5050);
    4. pushButton_abfragen->setText(tr("Abfragen ......"));
    5. pushButton_upload->setEnabled(false);
    6. pushButton_abfragen->setEnabled(false);
    7.  
    8. };//connectToServer
    9.  
    10. void sendRequest(void){
    11.  
    12. QByteArray block;
    13. QDataStream out(&block, QIODevice::WriteOnly);
    14. out.setVersion(QDataStream::Qt_4_2);
    15. QString abfrage("SELECT * FROM Datum; \r\n");
    16.  
    17.  
    18. out << abfrage.toAscii();
    19. out.device()->seek(0);
    20. out << quint16(block.size() - sizeof(quint16));
    21. tcpsocket.write(block);
    22. pushButton_abfragen->setText(tcpsocket.errorString());
    23.  
    24. };//sendRequest
    25.  
    26. void getData(void){
    27. pushButton_abfragen->setText(tr("Abfragen 2......"));
    28. //pushButton_abfragen->setText(tcpsocket.errorString()+" --1");
    29.  
    30. QFile confdatei("testdata.txt");
    31. confdatei.open(QIODevice::WriteOnly);
    32. QDataStream conf(&confdatei);
    33. QByteArray ip,wert,datum;
    34. conf.setVersion(QDataStream::Qt_4_2);
    35.  
    36. //pushButton_abfragen->setText(tcpsocket.errorString()+" --2");
    37.  
    38. QDataStream in(&tcpsocket);
    39. in.setVersion(QDataStream::Qt_4_2);
    40.  
    41. //pushButton_abfragen->setText(tcpsocket.errorString()+" --3");
    42.  
    43. forever{
    44.  
    45. if(nextBlockSize == 0){
    46. if(tcpsocket.bytesAvailable() < sizeof(quint16)){ break; }
    47. in >> nextBlockSize;
    48. }
    49. if(nextBlockSize == 0xFFFF){
    50. closeConnection();
    51. break;
    52. }
    53. if(tcpsocket.bytesAvailable() < nextBlockSize){ break;}
    54.  
    55. conf << in;//ip << wert << datum;
    56.  
    57.  
    58. nextBlockSize = 0;
    59. }
    60.  
    61.  
    62.  
    63.  
    64.  
    65. };//getData
    66.  
    67. void closeConnection(void){
    68.  
    69. tcpsocket.close();
    70. pushButton_abfragen->setText(tr("Daten abfragen"));
    71. pushButton_upload->setEnabled(true);
    72. pushButton_abfragen->setEnabled(true);
    73.  
    74. };//closeConnection
    75.  
    76. void displayError(QAbstractSocket::SocketError socketError)
    77. {
    78. switch (socketError) {
    79. case QAbstractSocket::RemoteHostClosedError:
    80. break;
    81. case QAbstractSocket::HostNotFoundError:
    82. QMessageBox::information(this, tr("Fortune Client"),
    83. tr("The host was not found. Please check the "
    84. "host name and port settings."));
    85. pushButton_abfragen->setText(tr("Daten abfragen"));
    86. pushButton_upload->setEnabled(true);
    87. pushButton_abfragen->setEnabled(true);
    88. break;
    89. case QAbstractSocket::ConnectionRefusedError:
    90. QMessageBox::information(this, tr("Fortune Client"),
    91. tr("The connection was refused by the peer. "
    92. "Make sure the fortune server is running, "
    93. "and check that the host name and port "
    94. "settings are correct."));
    95. pushButton_abfragen->setText(tr("Daten abfragen"));
    96. pushButton_upload->setEnabled(true);
    97. pushButton_abfragen->setEnabled(true);
    98. break;
    99. default:
    100. QMessageBox::information(this, tr("Fortune Client"),
    101. tr("The following error occurred: %1.")
    102. .arg(tcpsocket.errorString()));
    103. pushButton_abfragen->setText(tr("Daten abfragen"));
    104. pushButton_upload->setEnabled(true);
    105. pushButton_abfragen->setEnabled(true);
    106. }
    107.  
    108.  
    109. }
    To copy to clipboard, switch view to plain text mode 

    tcpsocket and nextBlockSize are member variables. And all connect - calls are made.
    I think the problem is the line:
    Qt Code:
    1. QString abfrage("SELECT * FROM Datum; \r\n");
    To copy to clipboard, switch view to plain text mode 
    If I send this line the readyRead() signal is emitted, and then direkt the disconnected() signal in response. I never get a error by the displayError slot.
    I thought the prolem is \r\n and I try to use \0 and single \n and so on.
    But it is all the same.
    So what's wrong? At the moment I have no idea what I can try out.
    Greetings,
    SuperSonik

  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: problem with QTcpSocket

    Maybe the database causes the disconnection? My guess is that you shouldn't use QDataStream, it does something more than just stream the data. Try using QTextStream instead.

    Anyway, maybe you should write your own database driver for that database?

  3. #3
    Join Date
    Jan 2007
    Posts
    7
    Thanks
    2
    Platforms
    Unix/X11 Windows

    Default Re: problem with QTcpSocket

    Hi,
    first thanks for your answer. You are the first in 3 forums who gives me a hint in 4 days!
    I try to use QTextStream and write what happens. -> Must I chance QByteArray ?
    Thanks,
    SuperSonik

  4. #4
    Join Date
    Jan 2007
    Posts
    7
    Thanks
    2
    Platforms
    Unix/X11 Windows

    Default Re: problem with QTcpSocket

    Hi,
    I once again . How can I write with QTextStream on QTcpSocket?
    Can someone give me an example?
    Greetings,

    SuperSonik

  5. #5
    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: problem with QTcpSocket

    Quote Originally Posted by SuperSonik View Post
    You are the first in 3 forums who gives me a hint in 4 days!
    As you see we have pretty good answer times here.
    I try to use QTextStream and write what happens. -> Must I chance QByteArray ?
    Open the text stream to the byte array and stream the QString contents through. You should end up with a byte array which you can then pass to QTcpSocket::write().

  6. #6
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem with QTcpSocket

    Quote Originally Posted by SuperSonik View Post
    Qt Code:
    1. void sendRequest(void) {
    2. QByteArray block;
    3. QDataStream out(&block, QIODevice::WriteOnly);
    4. out.setVersion(QDataStream::Qt_4_2);
    5. QString abfrage("SELECT * FROM Datum; \r\n");
    6.  
    7. out << abfrage.toAscii();
    8. out.device()->seek(0);
    9. out << quint16(block.size() - sizeof(quint16));
    10. tcpsocket.write(block);
    11. pushButton_abfragen->setText(tcpsocket.errorString());
    12. };//sendRequest
    To copy to clipboard, switch view to plain text mode 
    I'm not sure if this code is really what you wanted to do. toAscii() method converts QString to QByteArray. QByteArray written to QDataStream is represented as array size (quint32) followed by the array bytes. So in block variable you have something like this:
    Qt Code:
    1. [x][x][x][x][S][E][L][E][C][T][...]
    To copy to clipboard, switch view to plain text mode 
    and then you rewind to position 0 and overwrite first two bytes:
    Qt Code:
    1. [y][y][x][x][S][E][L][E][C][T][...]
    To copy to clipboard, switch view to plain text mode 
    Is this realy what you wanted?
    The Wheel weaves as the Wheel wills.

  7. #7
    Join Date
    Jan 2007
    Posts
    7
    Thanks
    2
    Platforms
    Unix/X11 Windows

    Default Re: problem with QTcpSocket

    Hi,
    I'm not sure. I read a Qt book, and there I read I have to write the lenght of my message first
    in my tcp package as a qint64.
    I took the code out of the book and chance a few things.
    In real I only want to send the string as I write it in the QString. -> exactly this String
    I thought to write the lenght before the message is something I have to do because of tcp.
    Greetings,
    SuperSonik
    Last edited by SuperSonik; 31st January 2007 at 10:11.

  8. #8
    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: problem with QTcpSocket

    Take a look at the docs, you'll notice the write() method that takes a byte array as its argument (it's probably defined in QIODevice class).

  9. The following user says thank you to wysota for this useful post:

    SuperSonik (31st January 2007)

  10. #9
    Join Date
    Jan 2007
    Posts
    7
    Thanks
    2
    Platforms
    Unix/X11 Windows

    Default Re: problem with QTcpSocket

    Ok,
    I have found my problem. I think my programm is working for a while.
    The error message was wrong. If I call tcpsocket.errorString() and there is no error the funktion returns "Unknown error".
    Now it's working. Thank you to all.
    Greetings,
    SuperSonik

Similar Threads

  1. Problem with QTcpSocket and QDataStream
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 16th September 2006, 13:08
  2. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  3. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. QTcpSocket disconnection problem
    By erdi in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:50

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.