Featured post
c++ - Network send problem -
i try send data on network, server i've programmed doesen't data. code worked befor:
void mainwindow::send() { qbytearray qbarr; qdatastream qdstrm(&qbarr, qiodevice::writeonly); int icount = qlist->count(); qprogressdialog qprogrsdsend(qstring("sending..."), qstring("cancel"), 0, icount, this); qdstrm.setversion(qdatastream::qt_4_6); qprogrsdsend.setwindowmodality(qt::windowmodal); for(int = 0; < icount; i++) { if(qprogrsdsend.wascanceled()) break; qdstrm << (quint16)0; qdstrm << (*qlist)[i].data(); qdstrm.device()->seek(0); qdstrm << (quint16)(qbarr.size() - sizeof(quint16)); qprogrsdsend.setvalue(i); qtcpsoclient->write(qbarr); qtcpsoclient->flush(); qtcpsoclient->waitforbyteswritten(); qbarr.clear(); } qlblstatus2->settext("file send."); }
but takes many time send each elemt qlist. tried modify methode, first elements qlist has been saved in qbarr. , send file. code doesnt work:
void mainwindow::send() { qbytearray qbarr; qdatastream qdstrm(&qbarr, qiodevice::writeonly); int icount = qlist->count(); qprogressdialog qprogrsdsend(qstring("sending..."), qstring("cancel"), 0, icount, this); qdstrm.setversion(qdatastream::qt_4_6); qprogrsdsend.setwindowmodality(qt::windowmodal); qdstrm << (quint16)0; for(int = 0; < icount; i++) { if(qprogrsdsend.wascanceled()) break; qdstrm << (*qlist)[i].data(); qprogrsdsend.setvalue(i); } qdstrm.device()->seek(0); qdstrm << (quint16)(qbarr.size() - sizeof(quint16)); qtcpsoclient->write(qbarr); qtcpsoclient->flush(); qtcpsoclient->waitforbyteswritten(); qbarr.clear(); qlblstatus2->settext("file send."); }
and here methode use read data:
void qserverthread::onreadyread(void) { if(read == false) { read = true; emit reading(true); } while(!qtcpsoclient->atend()) { qdatastream qdstrmin(qtcpsoclient); qdatastream qdstrmout(qfile); qbytearray qbarrdata; quint16 qui16blocksize = 0; int iversion = qdstrmin.version(); qdstrmin.setversion(iversion); qdstrmout.setversion(iversion); if(qtcpsoclient->bytesavailable() < (int)sizeof(quint16)) break; qdstrmin >> qui16blocksize; if(qtcpsoclient->bytesavailable() < qui16blocksize) break; qdstrmin >> qbarrdata; qdstrmout << qbarrdata.data(); qfile->flush(); } read = false; emit reading(false); }
i hope can me. thanks
paul
couldn't problem in server? suppose connect onreadyread readyread singal of socket. signal emitted once per chunk of data received. if send data @ once, possible signal gets emitted once. suppose qtcpsoclient
socket. now, can see happening:
you ask bytesavailable() < somesize
before data arrived yet. in case read size, break out right after , on next read lost size information , read garbage.
this might not problem before send multiple short messages, , every message managed arrive before asked data size. bug still there though.
on sidenote. in original client code - why did flush()
, waitforbyteswritten()
after every write
? may reason why slow.
[edit: corrected based on sergey tachenov's comment]
- Get link
- X
- Other Apps
Comments
Post a Comment