Results 1 to 5 of 5

Thread: problem in transfer file with tcpip part 2

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    9

    Default Re: problem in transfer file with tcpip part 2

    i have try all the methods provided, but cannot function properly.
    the problem i hv face:
    1. the clientConnection->disconnect() signal wont emit unless i run the clientConnection->disconnectFromHost(). bcoz i dont know when the clientConnection has finished reading data from sender. so i cant get the correct timming to run clientConnection->disconnectFromHost().

    2. the tcpserver->newConnection() signal will emit again even the last connection data is being reading.

    3. i try readyRead(), but it wont emit at all.....

    how to know when the clientConnection(in server) is finished reading the data from sender?give me some example pls.

    maybe i m stupid.....
    Last edited by wei243; 9th March 2007 at 09:28.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: problem in transfer file with tcpip part 2

    A quick, dirty and untested implementation:
    Qt Code:
    1. class FileSender : public QTcpSocket {
    2. Q_OBJECT
    3. public:
    4. FileSender(QObject *parent) : QTcpSocket(parent){
    5. _ready = false;
    6. connect(this, SIGNAL(connected()), SLOT(onConnected()));
    7. connect(this, SIGNAL(disconnected()), SLOT(onDisconnected()));
    8. connect(this, SIGNAL(bytesWritten(qint64)), SLOT(onWritten(qint64)));
    9. }
    10. bool sendFile(QIODevice *file){
    11. if(!_ready) return false;
    12. _ready = false;
    13. device = file;
    14. sendChunk();
    15. }
    16. signals:
    17. void fileTransfered();
    18. void ready();
    19. private slots:
    20. void onConnected(){
    21. _ready = true;
    22. emit ready();
    23. }
    24. void onWritten(qint64 s){
    25. if(device->atEnd()){
    26. // file sent
    27. _ready = true;
    28. emit fileTransfered();
    29. emit ready();
    30. } else sendChunk();
    31. }
    32. void onDisconnected(){
    33. _ready = false;
    34. }
    35. private:
    36. QIODevice *device;
    37. bool _ready;
    38. void sendChunk(){
    39. QByteArray ba = device->read(64000); // about 64kB
    40. qint64 writ = write(ba); // write data to socket
    41. if(writ<ba.size()){ // not all written
    42. for(int i=writ;i<ba.size(); i++) device->ungetChar(ba[i]); // write back to the file buffer
    43. }
    44. }
    45. };
    46.  
    47.  
    48. FileSender *fs = new FileSender(this);
    49. connect(fs, SIGNAL(ready()), this, SLOT(send()));
    50. connect(fs, SIGNAL(fileTransfered()), this, SLOT(finish()));
    51. fs->connectToHost("...", 3456);
    52.  
    53. //...
    54. // slot send():
    55. QFile *file = new QFile("...");
    56. file->open(QFile::ReadOnly);
    57. disconnect(fs, SIGNAL(ready()), this, SLOT(send()));
    58. fs->sendFile(file);
    59. //...
    60. // slot finish():
    61. file->close();
    62. delete file;
    63. connect(fs, SIGNAL(disconnected()), fs, SLOT(deleteLater())); // so that fs deletes itself
    64. fs->disconnectFromHost();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Problem when using qmake for .vcproj file generation
    By Conel in forum Qt Programming
    Replies: 3
    Last Post: 4th December 2006, 15:27
  2. OpenOffice file to QTextEdit (Unzip Problem)
    By patrik08 in forum Qt Programming
    Replies: 6
    Last Post: 27th November 2006, 11:32
  3. QHttp "PUT METHOD" QT Problem File Upload. RFC 2616
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2006, 23:02
  4. Problem with reading a file
    By Buhmann in forum Qt Programming
    Replies: 11
    Last Post: 17th February 2006, 14:02
  5. QProcess problem with windows batch file
    By bood in forum Qt Programming
    Replies: 11
    Last Post: 6th January 2006, 09:08

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.