The legend of a file...
First look at the sent ..
Header file
Header. h
# Ifndef header_h <br/> # define header_h <br/> # include <qdialog> <br/> # include <qtcpsocket> <br/> # include <q1_actsocket> <br/> class qtcpsocket; <br/> class qdialogbuttonbox; <br/> class qdialog; <br/> class qfile; <br/> class qprogressbar; <br/> class qlabel; <br/> class qvboxlayout; <br/> class qtimer; <br/> class dialog: Public qdimer {<br/> q_object <br/> public: <br/> Dialog (qwidget * parent = 0); <br/> Public slots: <br/> void start (); <br/> void quit (); <br/> void starttransfer (); <br/> void updateclientprogress (qint64 numbytes); <br/> void displayerror (q1_actsocket: socketerror ); <br/> void openfile (); <br/> void connecterror (); <br/> PRIVATE: <br/> qprogressbar * clientprogressbar; <br/> qlabel * clientstatuslabel; <br/> qpushbutton * startbutton; <br/> qpushbutton * quitbutton; <br/> qpushbutton * openbutton; <br/> using * buttonbox; <br/> qvboxlayout * mainlayout; <br/> qtcpsocket tcpclient; <br/> int flag; <br/> qint64 totalbytes; <br/> qint64 byteswritten; <br/> qint64 bytestowrite; <br/> qint64 localsize; <br/> qtimer * timer; <br/> qstring filename; <br/> qfile * localfile; <br/> qbytearray outblock; <br/>}; <br/> # endif // header_h <br/>
Main. cpp
# Include <qtgui> <br/> # include <qhostaddress> <br/> # include "header. H "<br/> // some initialization operations of the constructor <br/> dialog: Dialog (qwidget * parent): qdialog (parent) {<br/> localsize = 4*1024; <br/> totalbytes = 0; <br/> byteswritten = 0; <br/> bytestowrite = 0; <br/> setwindowtitle (TR ("Send File"); <br/> clientprogressbar = new qprogressbar; <br/> clientstatuslabel = new qlabel (TR ("client preparation completed"); <br/> startbutton = new qpushbutton (TR ("START" ); <Br/> quitbutton = new qpushbutton (TR ("quit"); <br/> openbutton = new qpushbutton (TR ("open ")); <br/> startbutton-> setenabled (false); <br/> buttonbox = new qdialogbuttonbox; <br/> buttonbox-> addbutton (startbutton, qdialogbuttonbox: actionrole ); <br/> buttonbox-> addbutton (openbutton, qdialogbuttonbox: actionrole); <br/> buttonbox-> addbutton (quitbutton, qdialogbuttonbox: rejectrole ); <br/> mainlayout = new qvb Oxlayout; <br/> mainlayout-> addwidget (clientprogressbar); <br/> mainlayout-> addwidget (clientstatuslabel); <br/> mainlayout-> addwidget (buttonbox ); <br/> setlayout (mainlayout); <br/> connect (startbutton, signal (clicked (), this, slot (START ())); <br/> connect (openbutton, signal (clicked (), this, slot (openfile (); <br/> connect (quitbutton, signal (clicked ()), this, slot (quit (); <br/> timer = new qtimer (this); <Br/> // connect (timer, signal (timeout (), this, slot (connecterror (); <br/> connect (& tcpclient, signal (connected (), this, slot (starttransfer (); <br/> connect (& tcpclient, signal (byteswritten (qint64), this, slot (updateclientprogress (qint64); <br/> connect (& tcpclient, signal (error (q1_actsocket: socketerror), this, slot (displayerror (q1_actsocket :: socketerror); <br/> flag = 1; <br/>}< br/> // connection timeout error <B R/> void dialog: connecterror () {<br/>}< br/> // exit <br/> void dialog: Quit () {<br/> qapp-> quit (); <br/>}< br/> // start to connect to the server <br/> void dialog: Start () {<br/> timer-> Start (1000); <br/> startbutton-> setenabled (false); <br/> qapplication: setoverridecursor (QT: waitcursor ); <br/> byteswritten = 0; <br/> clientstatuslabel-> settext (TR ("connected"); <br/> tcpclient. connecttohost (qhostaddress: localhost, 8888); <br/>}< B R/> // open the file <br/> void dialog: openfile () {<br/> filename = qfiledialog: getopenfilename (this); <br/> If (! Filename. isempty () {<br/> startbutton-> setenabled (true); <br/>}< br/> void dialog: starttransfer () {<br/> // open the file in read-only mode <br/> localfile = new qfile (filename); <br/> If (! Localfile-> open (qfile: readonly) {<br/> qmessagebox: Warning (this, TR ("application"), TR ("file cannot be read "). arg (filename ). arg (localfile-> errorstring (); <br/> return; <br/>}< br/> // obtain the file size <br/> totalbytes = localfile-> size (); <br/> // The sending buffer outblock is encapsulated in a qdatastream type variable <br/> qdatastream sendout (& outblock, qiodevice: writeonly ); <br/> // set the latest data version <br/> sendout. setversion (qdatastream: qt_4_6); <br/> // pass. right, remove the file path, <br/> // filename. lastindexof ('/') returns the number of digits (INT) of the last file/<br/> // filename. right () returns the number of digits starting from the right. <br/> // For example, if the path is D:/My Documents documents/tcp/header. h <br/> // filename. lastindexof ('/') returns the number of digits from D to the last slash (/). <br/> // filename. right (8) indicates the string starting from the right side of the path to the eighth digit <br/> qstring currentfile = filename. right (filename. size ()-filename. lastindexof ('/')-1); <br/> // construct a temporary file header to temporarily set the total length and file name length to 0 <br/> sendout <qint64 (0) <qint64 (0) <currentfile; <br/> // obtain the actual storage size of the file header and append it to totalbytes. <br/> totalbytes + = outblock. size (); <br/> // point the read/write operation to the file header. <br/> sendout. device ()-> seek (0); <br/> // enter the actual total length and file length <br/> sendout <totalbytes <qint64 (outblock. size ()-sizeof (qint64) * 2); <br/> // call the write function to change the bytestowrite when the file header is issued; <br/> bytestowrite = TotalBytes-tcpClient.write (outblock ); <br/> clientstatuslabel-> settext (TR ("connected"); <br/> qdebug () <currentfile <totalbytes; <br/> // clear the buffer <br/> outblock. resize (0); <br/>}< br/> // The byteswritten () is generated when data is sent () the signal then calls this method parameter to indicate the actually sent bytes <br/> void dialog: updateclientprogress (qint64 numbytes) {<br/> byteswritten + = (INT) numbytes; <br/> If (bytestowrite> 0) {<br/> outblock = localfile-> Read (qmin (bytestowrite, localsize); <br/> bytestowrite-= (INT) tcpclient. write (outblock); <br/> outblock. resize (0); <br/>}else {<br/> localfile-> close (); <br/>}< br/> clientprogressbar-> setmaximum (totalbytes ); <br/> clientprogressbar-> setvalue (byteswritten); <br/> clientstatuslabel-> settext (TR ("sent % 1 m "). arg (byteswritten/(1024*1024); <br/>}< br/> // link error method <br/> void dialog: displayerror (q1_actsocket :: socketerror) {<br/> If (socketerror = qtcpsocket: remotehostclosederror) <br/> return; <br/> qmessagebox: Information (this, TR ("network"), TR ("produces the following error: % 1. "). arg (tcpclient. errorstring (); <br/> tcpclient. close (); <br/> clientprogressbar-> Reset (); <br/> clientstatuslabel-> settext (TR ("client ready ")); <br/> startbutton-> setenabled (true); <br/> qapplication: restoreoverridecursor (); <br/>}< br/> int main (INT argc, char ** argv) {<br/> qapplication app (argc, argv); <br/> qtextcodec: setcodecfortr (qtextcodec: codecforname ("gb2312 ")); <br/> dialog; <br/> dialog. show (); <br/> return app.exe C (); <br/>}< br/>
View accepted files again
Header. h
# Ifndef dialog_h <br/> # define dialog_h <br/> # include <qdialog> <br/> # include <qtcpserver> <br/> class qfile; <br/> class qdialogbuttonbox; <br/> class qlabel; <br/> class qprogressbar; <br/> class qpushbutton; <br/> class qtcpsocket; <br/> class dialog: Public qdialog <br/>{< br/> q_object <br/> Public: <br/> Dialog (qwidget * parent = 0 ); <br/> Public slots: <br/> void start (); <br/> void acceptconnection (); <br/> void updateserverprogress (); <br/> void displayerror (q1_actsocket: socketerror); <br/> PRIVATE: <br/> qprogressbar * clientprogressbar; <br/> qprogressbar * serverprogressbar; <br/> qlabel * serverstatuslabel; <br/> qpushbutton * startbutton; <br/> qpushbutton * quitbutton; <br/> qpushbutton * openbutton; <br/> qalogbuttonbox * buttonbox; <br/> qtcpserver tcpserver; <br/> qtcpsocket * tcpserverconnection; <br/> qint64 totalbytes; <br/> qint64 bytesreceived; <br/> qint64 filenamesize; <br/> qstring filename; <br/> qfile * localfile; <br/> qbytearray inblock; <br/>}; <br/> # endif <br/>
View the implementation file again
Dialog. cpp
# Include "cycler. H "<br/> # include <qtnetwork> <br/> receiver: Receiver (qobject * parent) <br/>: qobject (parent) <br/>{< br/> udpsocket = new qudpsocket (this); <br/> udpsocket-> BIND (44444); <br/> connect (udpsocket, signal (readyread (), <br/> This, slot (processpendingtransferrams (); <br/>}</P> <p> void uploer: processpendingtransferrams () <br/>{< br/> while (udpsocket-> haspendingdatagrams () {<br/> qbytearray datasync; <br/> datax. resize (udpsocket-> pending1_ramsize (); <br/> udpsocket-> readdatax (datax. data (), datax. size (); <br/> qdebug () <(TR ("received data:/" % 1/"") <br/>. arg (datax. data (); <br/>}< br/>}
Then main. cpp
# Include <qapplication> <br/> # include <qtcore> <br/> # include "dialog. H "<br/> int main (INT argc, char * argv []) <br/>{< br/> qapplication app (argc, argv ); <br/> qtextcodec: setcodecfortr (qtextcodec: codecforname ("gb2312"); <br/> dialog. show (); <br/> return dialog.exe C (); <br/>}< br/>
The execution result is as follows ..