Results 1 to 8 of 8

Thread: Can't get this to work... [QTcpSocket]

  1. #1
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Question Can't get this to work... [QTcpSocket]

    Hi,
    I'm trying to create an app for my Symbian phone. It should receive a list with filenames from the Server which is running on my Windows PC, and my phone should put each filename as a new Item in the QWidgetList. The problem is that when my phone receives the filenames it puts some filenames together. E.g:
    First Item: Like the way.mp3
    Secon Item: Pump it up.mp3blabla.mp3
    Third Item: blabla.mp3

    I don't know why this happens. I also read the "Fortune Client" Example, but I couldn't really understand it.

    Qt Code:
    1. void Client::readFortune()
    2. {
    3. /*ui->statusLabel->setText("Connected!");
    4.   QDataStream in(tcpSocket);
    5.   in.setVersion(QDataStream::Qt_4_0);
    6.  
    7.   char buffer[200] = "";
    8.   tcpSocket->read(buffer, tcpSocket->bytesAvailable());
    9.   ui->listWidget->addItem(buffer);*/
    10.  
    11. QDataStream in(tcpSocket);
    12. in.setVersion(QDataStream::Qt_4_0);
    13. QByteArray array1 = tcpSocket->read(100);
    14. ui->listWidget->addItem(array1);
    15.  
    16. //statusLabel->setText(tr("Connected"));
    17. }
    To copy to clipboard, switch view to plain text mode 

    The function is already connected with "readyRead".

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't get this to work... [QTcpSocket]

    Hi there!

    You need to make use of the tcpSocket->bytesAvailable(). And I think you need to keep track of your packet length, because its not guaranteed to arrive in one piece:

    writing:

    Qt Code:
    1. // fill your data...
    2. QDataStream out(tcpSocket);
    3. out << data.size() << data;
    To copy to clipboard, switch view to plain text mode 
    reading: You need a member variable in client: int nextBlockSize

    Qt Code:
    1. void Client::readFortune()
    2. {
    3. QDataStream in(tcpsocket);
    4. in.setVersion(QDataStream::Qt_4_0);
    5. if (nextBlockSize == 0) {
    6. if (tcpsocket->bytesAvailable() < sizeof(int)) return;
    7. in >> nextBlockSize;
    8. }
    9. if ((int)this->bytesAvailable() < nextBlockSize) return;
    10. QByteArray data = tcpsocket->read(nextBlockSize);
    11. // do something with your data..
    12.  
    13. // Prepare for next block
    14. nextBlockSize = 0;
    15. }
    To copy to clipboard, switch view to plain text mode 
    If there is a more elegant solution I would be happy to hear about it - But this one should work!

    Joh

    PS: I didn't compile the code.. pasted it from an old project and simplified it. there could be some syntax errors :->

  3. The following user says thank you to JohannesMunk for this useful post:

    haggard433 (14th September 2010)

  4. #3
    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: Can't get this to work... [QTcpSocket]

    Quote Originally Posted by JohannesMunk View Post
    If there is a more elegant solution I would be happy to hear about it - But this one should work!
    If all you have to transfer is filenames then you can ignore QDataStream and read the data line by line:
    Qt Code:
    1. void X::onSocketReadyRead() {
    2. while(socket->canReadLine()){
    3. // you can use QTextStream if you want
    4. // to use stuff such as text codecs
    5. // but here let's read it directly:
    6. QString fileName = socket->readLine();
    7. doSomethingWith(fileName);
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Of course the data needs to be piped into the socket in a similar manner.

    Edit: Oh... make sure to have filenames shorter than the TCP window size+socket buffer on your machine or else both presented solutions will fail badly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following 2 users say thank you to wysota for this useful post:

    haggard433 (14th September 2010), JohannesMunk (14th September 2010)

  6. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't get this to work... [QTcpSocket]

    Hi Wysota!

    Yes, thats more elegant for strings!

    Is my solution really going to fail for big dataArrays? Wouldn't the system split up / fragment the packet automatically? And my read routine waits till all parts have arrived.

    I will have to try or make use of that in the near future. I'll report back.

    Of course, if the packet is bigger than the socket buffer, than this fails too. But the default QAbstractSocket::readBufferSize() is 0, which corresponds to no limit.

    Joh

  7. #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: Can't get this to work... [QTcpSocket]

    Quote Originally Posted by JohannesMunk View Post
    Is my solution really going to fail for big dataArrays? Wouldn't the system split up / fragment the packet automatically? And my read routine waits till all parts have arrived.
    If you don't read data from the socket, eventually it will fill its buffer and perform network flow control by reducing the TCP window size to 0 thus preventing the transmitting end from sending more data. If the amount of data you expect is not received by then, it never will be.

    Of course, if the packet is bigger than the socket buffer, than this fails too. But the default QAbstractSocket::readBufferSize() is 0, which corresponds to no limit.
    If you don't read data, it will eventually stop flowing -- see for yourself (try sending a 1GB file through a socket and don't start reading it on the receiving end). If that wasn't the case, it would be a perfect denial of service attack opportunity. Unless they changed this behaviour since a couple of releases ago which could be true.

    A proper approach would be to read all incoming data into your own buffer and check this buffer for the data you expect. But as I said - maybe they changed something in the socket code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #6
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Can't get this to work... [QTcpSocket]

    Thanks to both of you, my Phone if finally receiving the songnames! But since I had to "add" \n to the strings which are being sent by the server, my phone makes now a new empty line between the Items in the WidgetList. That isn't the biggest problem. Firstly I should explain what my app for my phone does: The PC (server) sends all filenames to my phone, the phone puts them in a QListWidget and when I double tap on one of them, the phone sends this filename back to the server and the PC then plays this song which is located on PC. It's like a remote control for my PC. But since I had to add the "\n" it also sends the filename with the "\n" back to the server, and the audio library can't play the music file. Is there a way to "take away" the "\n" after the filenames have reached the phone?

  9. #7
    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: Can't get this to work... [QTcpSocket]

    Quote Originally Posted by haggard433 View Post
    Is there a way to "take away" the "\n" after the filenames have reached the phone?
    See QString::trimmed().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    haggard433 (14th September 2010)

  11. #8
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Can't get this to work... [QTcpSocket]

    That was exactly what I was looking for! Thank you very much!

Similar Threads

  1. Replies: 2
    Last Post: 13th December 2009, 21:27
  2. Need Help with QTcpSocket
    By jknotzke in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2009, 14:55
  3. QTcpSocket help
    By tsd-charlie in forum Newbie
    Replies: 1
    Last Post: 6th August 2009, 20:40
  4. QTcpSocket
    By pdoria in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2009, 18:52
  5. Using QTcpSocket
    By mraittin in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2007, 10:54

Tags for this Thread

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.