Results 1 to 7 of 7

Thread: TCP server-client app problem

  1. #1
    Join Date
    Jan 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default TCP server-client app problem

    Any idea what's wrong with my TCP server-client app? I've been copying the TCP Trip Planner example in the book (so no threads used). The difference is that I've changed it so that it doesn't close the connection after one exchange of data between client/server. It works at first; client sends a request and gets a response, but after that the response is always empty (integers come in as 0 and strings as ""). I've got a feeling there's a simple solution and I'm just doing this wrong.

    Here's the code. Well, the relevant part (I hope). Every signal and slot is connected appropriately.

    Client:
    Qt Code:
    1. QTcpSocket tcpSocket;
    2. quint16 nextBlockSize;
    3. quint8 mode;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //constructor
    2. connect(&tcpSocket, SIGNAL(readyRead()), this, SLOT(readServer()));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //connectToServer()
    2. tcpSocket.connectToHost("127.0.0.1", 40567);
    3. nextBlockSize = 0;
    4.  
    5. mode = 4;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //sendRequest()
    2. QByteArray block;
    3. QDataStream out(&block, QIODevice::WriteOnly);
    4. out.setVersion(QDataStream::Qt_4_4);
    5. out << quint16(0) << quint8('S') << mode;
    6. out.device()->seek(0);
    7. out << quint16(block.size() - sizeof(quint16));
    8. tcpSocket.write(block);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //readServer()
    2. DataStream in(&tcpSocket);
    3. in.setVersion(QDataStream::Qt_4_4);
    4.  
    5. for(;;)
    6. {
    7. if(nextBlockSize == 0)
    8. {
    9. if(tcpSocket.bytesAvailable() < sizeof(quint16))
    10. break;
    11. in >> nextBlockSize;
    12. }
    13.  
    14. if(nextBlockSize == 0xFFFF)
    15. {
    16. //closeConnection();
    17. break;
    18. }
    19.  
    20. if(tcpSocket.bytesAvailable() < nextBlockSize)
    21. break;
    22.  
    23. quint8 modev;
    24.  
    25. in >> modev;
    26.  
    27. switch(modev)
    28. {
    29. case 4:
    30. //stuff
    31. break;
    32. }
    33.  
    34. nextBlockSize = 0;
    35. }
    To copy to clipboard, switch view to plain text mode 
    Server side (after creating a new object to handle the connection with the client):
    Qt Code:
    1. //reading and writing
    2. QDataStream in(this);
    3. in.setVersion(QDataStream::Qt_4_4);
    4.  
    5. if(nextBlockSize == 0)
    6. {
    7. if(bytesAvailable() < sizeof(quint16))
    8. return;
    9. in >> nextBlockSize;
    10. }
    11.  
    12. if(bytesAvailable() < nextBlockSize)
    13. return;
    14.  
    15. quint8 requestType;
    16. quint8 mode;
    17.  
    18. in >> requestType;
    19. if(requestType == 'S')
    20. {
    21. in >> mode;
    22.  
    23. if(mode == 4)
    24. {
    25. QByteArray block;
    26. QDataStream out(&block, QIODevice::WriteOnly);
    27. out.setVersion(QDataStream::Qt_4_4);
    28. out << quint16(0) << mode;
    29. out.device()->seek(0);
    30. out << quint16(block.size() - sizeof(quint16));
    31.  
    32. write(block);
    33. }
    34. }
    35.  
    36. nextBlockSize = 0;
    To copy to clipboard, switch view to plain text mode 
    Last edited by pogostick; 24th January 2010 at 18:38.

  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: TCP server-client app problem

    Qt uses sockets in asynchronous manner, it doesn't block on read or write calls thus you can't write to the socket and immediately start reading from it expecting a response to be already there.
    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.


  3. #3
    Join Date
    Jan 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: TCP server-client app problem

    I'm sorry, I didn't make it very clear. Those three pieces of code (starting with the comment lines) are actually separate functions. First the connection is established, then a request is sent when the user e.g. presses a button, and finally when the readyRead() signal is emitted the client reads the data. There's just so much stuff in between I thought it'd look more clear.

  4. #4
    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: TCP server-client app problem

    So which part is where?
    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. #5
    Join Date
    Jan 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: TCP server-client app problem

    Okay, I edited the first post.

  6. #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: TCP server-client app problem

    It's hard to find the error in such code if you don't see the context. I suggest you inject calls to QDataStream::status() to see which part of the spaghetti... eeem... I mean... code gets executed and what breaks the stream.
    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.


  7. #7
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: TCP server-client app problem

    @pogostick:
    Please show us the complete source of the client and the server. You are not showing complete methods, so there is no way that we ever know what you are doing outside what you're showing us.
    Maybe the problem is beacuse of variables going out of scope. But this way will we never know.

    That said, please check if you're not close()ing the connection in the server after sending a block of data, as the code in your book does, C++ GUI programming with Qt4 (ISBN 0-13-187249-4).

Similar Threads

  1. Qt-- Client Server communication problem
    By thisismyuname in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2010, 01:04
  2. Client Server
    By electronicboy in forum General Programming
    Replies: 3
    Last Post: 29th October 2009, 10:10
  3. password problem with client and server
    By mate in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2008, 18:20
  4. client-server how?
    By nongentesimus in forum Newbie
    Replies: 6
    Last Post: 28th November 2006, 09:25

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.