c++ - Qt client programme -
i wrote program client on qt receive data server not receiving data , showing received bytes zero,following program:
//client.h
#ifndef client_h #define client_h #include <qobject> #include <qstring> #include <qtnetwork/qtcpsocket> class client: public qobject { q_object public: client(qobject* parent = 0); ~client(); void start(qstring address, quint16 port); void send(const char*); void receive(); public slots: void starttransfer(); private: qtcpsocket client; }; #endif // client_h
//client.cpp
#include "client.h" #include <qtnetwork/qhostaddress> #include<qiodevice> client::client(qobject* parent): qobject(parent) { connect(&client, signal(connected()), this, slot(starttransfer())); //connect(&client, signal(waitforbyteswritten()), // this, slot(receive())); } client::~client() { client.close(); } void client::start(qstring address, quint16 port) { qhostaddress addr(address); client.connecttohost(addr, port); } void client::starttransfer() { client.write("connection established", 22); } void client::send(const char *buffer) { client.write(buffer,sizeof(buffer)); } void client::receive() { char temp[1024] = {0}; int len = client.read(temp,client.bytesavailable()); printf("\tdata recieved server :: %s\n",temp); printf("\tsize of data received :: %d\n",client.bytesavailable()); printf("\tbytes read :: %d\n",len); }
//main.cpp
#include <qcoreapplication> #include "client.h" //#include <qapplication> int main(int argc, char *argv[]) { qcoreapplication a(argc, argv); client client; client.start("192.168.1.2", 9602); char buff[] = "send operation performed main"; client.send(buff); // while(1) client.receive(); return a.exec(); }
here program function executes , stops receiving(may be),when send thing server doesn't take anything.any suggestions? plz don't rude if have done silly programming mistake because i'm newbie.
you not getting @merlin069's answer....you should use readyread in signal's place , receieve function slot...it work.i hope easy language understandable you.
Comments
Post a Comment