Results 1 to 7 of 7

Thread: TCP server-client app problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.