Results 1 to 5 of 5

Thread: problem in transfer file with tcpip part 2

  1. #1
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    9

    Default problem in transfer file with tcpip part 2

    special thx to wysota, help me solve most of my problem.
    now i manage to send any file form QTcpSocket to QTcpServer within 1 standalone pc.

    but if i transfer a file within a network, the receiver only receive PARTIALLY, except the file size is very small.
    how to slove this problem?

    below is my sender program with QTcpSocket:
    Qt Code:
    1. void sender::sending()
    2. {
    3. tcpSocket1 = new QTcpSocket(this);
    4. tcpSocket1->abort();
    5.  
    6. tcpSocket1->connectToHost(targetIP,targetPort);
    7.  
    8. file.setFileName(fileName);
    9. file.open(QIODevice::ReadOnly);
    10.  
    11. QByteArray Data;
    12. Data = file.readAll();
    13.  
    14. tcpSocket1->write(Data);
    15.  
    16. connect(tcpSocket1, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
    17. connect(tcpSocket1, SIGNAL(disconnected()),tcpSocket1, SLOT(deleteLater()));
    18. connect(tcpSocket1, SIGNAL(disconnected()),this, SLOT(closeFile()));
    19. }
    20. void sender::closeFile()
    21. {
    22. file.close();
    23. }
    To copy to clipboard, switch view to plain text mode 

    receiver program with QTcpServer:
    Qt Code:
    1. void receiver::startserver()
    2. {
    3. tcpServer1 = new QTcpServer(this);
    4. if (!tcpServer1->listen(QHostAddress(ownIP),0)){
    5. QMessageBox::critical(0, tr("Listening...."),tr("Unable to start the server: ").arg(tcpServer1->errorString()));
    6. }
    7.  
    8. timer = new QTimer(this);
    9. connect(tcpServer1, SIGNAL(newConnection()), this, SLOT(startTimer()));
    10. connect(timer, SIGNAL(timeout()), this, SLOT(readIncoming()));
    11. }
    12. void receiver::startTimer()
    13. {
    14. timer->start(500);
    15. }
    16. void receiver::readIncoming()
    17. {
    18. timer->stop();
    19.  
    20. QTcpSocket *clientConnection = tcpServer1->nextPendingConnection();
    21. QApplication::setOverrideCursor(Qt::WaitCursor);
    22.  
    23. QByteArray Data = clientConnection->readAll();
    24.  
    25. file.setFileName(fileName);
    26. file.open(QIODevice::WriteOnly);
    27. file.write(Data);
    28.  
    29. connect(clientConnection, SIGNAL(disconnected()),clientConnection, SLOT(deleteLater()));
    30. connect(clientConnection, SIGNAL(disconnected()),this, SLOT(closeFile()));
    31.  
    32. clientConnection->disconnectFromHost();
    33.  
    34. QApplication::restoreOverrideCursor();
    35. }
    36. void receiver::closeFile()
    37. {
    38. file.close();
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wei243; 8th March 2007 at 07:34.

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in transfer file with tcpip part 2

    Well, it seems that timeout() signal is triggered too early, and if you transfer large files, they won't be send in 500ms through the network !

    What you could do, is connect your server to readyRead() signal, and just send a special ending message with your sender socket, to notify server that file is transferred.

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

    Default Re: problem in transfer file with tcpip part 2

    Quote Originally Posted by wei243 View Post
    but if i transfer a file within a network, the receiver only receive PARTIALLY, except the file size is very small.
    how to slove this problem?
    I already told you - Qt handles network in an asynchronous way:
    1. when connectToHost() returns, you are not connected to the remote host
    2. when write() returns, the data is not written to the socket
    3. Qt will emit signals when events happen and you can connect to those signals to handle events such as new connection, data is ready to read, data has been written, disconnected from host - no need to use any timers.

  4. #4
    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 08:28.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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, 14:27
  2. OpenOffice file to QTextEdit (Unzip Problem)
    By patrik08 in forum Qt Programming
    Replies: 6
    Last Post: 27th November 2006, 10:32
  3. QHttp "PUT METHOD" QT Problem File Upload. RFC 2616
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2006, 22:02
  4. Problem with reading a file
    By Buhmann in forum Qt Programming
    Replies: 11
    Last Post: 17th February 2006, 13:02
  5. QProcess problem with windows batch file
    By bood in forum Qt Programming
    Replies: 11
    Last Post: 6th January 2006, 08: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.