Results 1 to 12 of 12

Thread: Help with a simple client programe

  1. #1
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Help with a simple client programe

    Hi all,

    I'm coding a simple client-server communication programe, wherein I require to send a struct data to server. Is it possible to send the struct as a single packet to the server??
    if yes, then how to do it?

    any help will be apprecated.

    regards

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a simple client programe

    Hi,

    a good starting point for this is Qt Network Examples.
    For more detailed information have look at Qt Network Module.

    For serialization of data structures have a look at QDataStream.


    For further help - just ask!

    Good luck

    Tom

  3. #3
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a simple client programe

    will not that serialization of the structure data make it different packets, if I'll use QDataStream? , b'coz all the server is looking for is a single packet, which consists of certain header along with data like username.

    kindly throw some light on it pls.

    Thanks

  4. #4
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a simple client programe

    What do you mean by "single packet"?

    QDataStream will help you to keep your code base small and provides an easy-to-use way to handle serialization.

    QDataStream will help you to avoid nasty parsing and/or byte counting stuff.

  5. #5
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a simple client programe

    by 'single packet' I meant to say that, the packet structure consist of header, a key, and the data -- which is username. Now when we're going to use QDataStream, we'll be using "<<" operator to send each and every variable of the struct. so my query is will it send differently of just as serially as it is there, in the QDataStream, so that the server won't receive the struct in different format altogether at its end.

    Thanks

  6. #6
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a simple client programe

    Quote Originally Posted by QuTe View Post
    ... Now when we're going to use QDataStream, we'll be using "<<" operator to send each and every variable of the struct.
    ...
    You can stream a whole struct/class to QDataStream by defining stream operators for the struct/class.
    This is a little bit nicer than every time streaming the struct members.

    Good luck,

    Tom

    Here some pseudo-code to show what I mean:

    Qt Code:
    1. class SomeClass
    2. {
    3. ...
    4. private:
    5. int _mSomeInt;
    6. QString _mSomeString;
    7.  
    8. friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const SomeClass &);
    9. friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, SomeClass &);
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const SomeClass &c)
    2. {
    3. return out << (quint32)(c._mSomeInt) << c._mSomeString;
    4. }
    5.  
    6. QDataStream &operator>>(QDataStream &in, SomeClass &c)
    7. {
    8. quint32 i;
    9. in >> i;
    10. c._mSomeInt = i;
    11. in >> c._mSomeString;
    12. return in;
    13. }
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to DeepDiver for this useful post:

    QuTe (26th October 2007)

  8. #7
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a simple client programe

    Hi Tom
    Thanks for the helpful suggestions. But when I used the my own operator "<<" definition, its not detecting it somehow! i don't know whats wrong, but its throwing the error that "no match ' ' found", for the operator I'm using.
    I've declared the QDatastream operator the same way you've shown in the snippet above. but it still doesn't work for me.

    pls help.

    Thanks

  9. #8
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a simple client programe

    Attach the code and we can have a look!

    Tom

  10. #9
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a simple client programe

    ok here you go then:

    header file:
    Qt Code:
    1. #ifndef CLIENTSOCKET_H
    2. #define CLIENTSOCKET_H
    3.  
    4. #include <QTcpSocket>
    5. #include <QVBoxLayout>
    6. #include <QHBoxLayout>
    7. #include <QTextDocument>
    8. #include <QLineEdit>
    9. #include <QLabel>
    10. #include <QPushButton>
    11. #include <QTextStream>
    12. #include <qglobal.h>
    13. #include <QDataStream>
    14.  
    15.  
    16. struct clientPktStruct {
    17. QString pktString;
    18. qint32 size_of_pkt;
    19. qint8 key;
    20. QString uName;
    21. };
    22.  
    23.  
    24. class Client : public QWidget
    25. {
    26. Q_OBJECT
    27. public:
    28. Client(QWidget *parent=0);
    29.  
    30.  
    31. ~Client()
    32. {
    33. }
    34.  
    35. private slots:
    36. void closeConnection();
    37. void sendToServer();
    38. void socketReadyRead();
    39. void socketConnected();
    40. void socketConnectionClosed();
    41. void socketClosed();
    42. void displayError( QAbstractSocket::SocketError );
    43.  
    44. private:
    45. QPushButton *okay;
    46. QPushButton *cancel;
    47. QString strnglist;
    48. QString newChallengeString;
    49. QTcpSocket *socket;
    50. QFile *infoText;
    51. QDataStream Debug;
    52. QLabel *userNameLabel;
    53. QLabel *userPasswdLabel;
    54. QLineEdit *userNameText;
    55. QLineEdit *userPasswdText;
    56. struct clientPktStruct clientPkt;
    57.  
    58. quint16 port;
    59. quint16 blockSize;
    60. friend Q_CORE_EXPORT QDataStream &operator<< (QDataStream &, const struct clientPktStruct &);
    61. };
    62.  
    63. #endif
    To copy to clipboard, switch view to plain text mode 

    and the cpp file:
    Qt Code:
    1. #include <QtGui>
    2. #include <QLineEdit>
    3. #include <QBoxLayout>
    4. #include <QHostAddress>
    5.  
    6. #include "clientSocket.h"
    7.  
    8.  
    9. Client::Client(QWidget *parent)
    10. :QWidget(parent)
    11. {
    12.  
    13. // GUI layout
    14. QVBoxLayout *mainLayout = new QVBoxLayout(this);
    15.  
    16. QGridLayout *labelLayout = new QGridLayout;
    17. userNameLabel = new QLabel(tr("UserName:"), this);
    18. userPasswdLabel = new QLabel(tr("Password:"), this);
    19. userNameText = new QLineEdit(this);
    20. userPasswdText = new QLineEdit(this);
    21. userPasswdText->setEchoMode(QLineEdit::Password);
    22. labelLayout->addWidget(userNameLabel, 0, 0);
    23. labelLayout->addWidget(userNameText, 0, 1);
    24. labelLayout->addWidget(userPasswdLabel, 1, 0);
    25. labelLayout->addWidget(userPasswdText, 1, 1);
    26.  
    27. okay = new QPushButton( tr("ok") , this );
    28. cancel = new QPushButton( tr("cancel") , this );
    29.  
    30. hb->addWidget(okay);
    31. hb->addWidget(cancel);
    32.  
    33. mainLayout->addLayout(labelLayout);
    34. mainLayout->addStretch(1);
    35. mainLayout->addLayout(hb);
    36. setLayout(mainLayout);
    37. connect( okay, SIGNAL(clicked()), SLOT(sendToServer()) );
    38. connect( cancel, SIGNAL(clicked()), qApp, SLOT(quit()) );
    39.  
    40. // create the socket and connect various of its signals
    41. host.setAddress("203.187.156.70");
    42. port = 5000;
    43. socket = new QTcpSocket( this );
    44. connect( socket, SIGNAL(connected()),
    45. SLOT(socketConnected()) );
    46. connect( socket, SIGNAL(connectionClosed()),
    47. SLOT(socketConnectionClosed()) );
    48. connect( socket, SIGNAL(readyRead()),
    49. SLOT(socketReadyRead()) );
    50. connect( socket, SIGNAL(error(QAbstractSocket::SocketError)),
    51. SLOT(displayError(QAbstractSocket::SocketError)) );
    52.  
    53. // connect to the server
    54. clientPkt.pktString = "ISHR1.00";
    55. clientPkt.key = 101;
    56.  
    57. socket->connectToHost( host, port );
    58. }
    59.  
    60. ........
    61. ........
    62.  
    63. void Client::sendToServer()
    64. {
    65. // write to the server
    66. clientPkt.uName = userNameText->text();
    67. clientPkt.size_of_pkt = sizeof(clientPkt);
    68.  
    69. QDataStream sendData(socket);
    70. sendData.setVersion(QDataStream::Qt_4_0);
    71.  
    72. /* this is where the operator will be used */
    73. sendData<<clientPkt;
    74.  
    75. userNameText->setText( "" );
    76. okay->setEnabled(false);
    77. }
    78.  
    79. ..........
    80. ..........
    81. .........
    82.  
    83.  
    84. /* operator for datastreaming a structure field */
    85. QDataStream &operator<< (QDataStream &sendPkt, const struct clientPktStruct &CPkt)
    86. {
    87. return sendPkt <<CPkt.pktString<<CPkt.size_of_pkt<<CPkt.key<<CPkt.uName;
    88. }
    To copy to clipboard, switch view to plain text mode 


    after compilation the compiler throw the error::
    error: no match for 'operator <<' in 'sendData <<Client::clientPkt'

  11. #10
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a simple client programe

    oops my bad!

    I just realised what I did , I should have implemented the 'operator<<' before utilising it in the program. I mistakenly put it later.

    nevermind sry about that.

    Thanks alot for the help Tom

    Regards

  12. #11
    Join Date
    Oct 2007
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help with a simple client programe

    Now, everything is working fine, except the fact that, my server is not being able to recognize the protocol which we use( we put the a string to recognize the packets in its header). and I checked with the server data and I found that, actually the server is getting '0' s ( zeros) in between each of the character data I'm sending!! and btw I'm sending it using QDataStream.
    And I also tried with QByteArray, and well it works bit fine with it, as compared to QDataStream, but in QByteArray as well, I'm facing a problem, which is, my server doesn't get the size of the packet( which is one of the members of the packet structure which I'm sending from here)
    Now is there any other simple way to send data packets to server without getting the data manipulated while sending using Qt APIs???

    pls help

    Regards

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

    Default Re: Help with a simple client programe

    Quote Originally Posted by QuTe View Post
    I checked with the server data and I found that, actually the server is getting '0' s ( zeros) in between each of the character data I'm sending!!
    By default Qt sends strings in UTF-16 --- that's why each character is encoded using two bytes.

    Quote Originally Posted by QuTe View Post
    and btw I'm sending it using QDataStream.
    QDataStream uses its own protocol for serialization, see here.

    Quote Originally Posted by QuTe View Post
    Now is there any other simple way to send data packets to server without getting the data manipulated while sending using Qt APIs???
    Use QDataStream::writeRawData(), QDataStream::writeBytes() or QIODevice::write().

Similar Threads

  1. QTcpSocket Chat : Mutiple Client using FD_SET and select()
    By cooler123 in forum Qt Programming
    Replies: 20
    Last Post: 15th January 2012, 19:57
  2. synching client readings to server output
    By OnionRingOfDoom in forum Qt Programming
    Replies: 14
    Last Post: 28th January 2006, 18:15

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.