Results 1 to 8 of 8

Thread: Transfert of File with QTcpSocket

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Transfert of File with QTcpSocket

    Dear freinds,

    I am facing a problem of transferring a file in a mini chat.
    After clicking on the upload buton, I get directory where the file is saved, but I can find file itself.

    Here is my code:

    Qt Code:
    1. void FenClient::sendFile()
    2. {
    3. QString fileName = QFileDialog::getOpenFileName(this, "Information", "Open a file",
    4. "File(*.text *.png *.jpeg *.gif)");
    5. m_file = fileName;
    6. QFile file(fileName);
    7.  
    8. if(!file.open(QIODevice::ReadOnly))
    9. {
    10. return;
    11. }
    12.  
    13. QByteArray paquet = file.readAll();
    14. QDataStream out(&paquet, QIODevice::WriteOnly);
    15. out<< (quint32) 0;
    16. out<< (quint32)(file.size()-sizeof(quint32));
    17. m_socket->write(paquet);
    18. }
    To copy to clipboard, switch view to plain text mode 

    Could you please tell me what is missing in my code and how to fix the problem?

    Many thanks.

  2. #2
    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: Transfert of File with QTcpSocket

    Eeeem.... what exactly did you expect to obtain with the above code?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Transfert of File with QTcpSocket

    I just want to download a file and to transfert it in a mini chat (Server/Client)

  4. #4
    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: Transfert of File with QTcpSocket

    Quote Originally Posted by Stanfillirenfro View Post
    I just want to download a file and to transfert it in a mini chat (Server/Client)
    This code does not "download" anything neither does it "transfer" anything to a "mini chat". What it does is that it reads a file and overwrites its first couple of bytes with two serialized integers one of which is 0 and the other is size of the file decreased by four. Then it writes that broken file to m_socket.

    Thus I'm asking you again to state what you expected this code to do. We already know what it does and we can assume you wanted it to do something else so please state what was the expected outcome of running that function.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Transfert of File with QTcpSocket

    Many thanks Wysota for your reply.

    This is the situation. I have written a code of a mini chat. In this code, I wanted to be able to transfert data to clients. What I have sent was just a piece of my code. This piece of code should open a directory, read a file, copy it and sent it to different cilents.
    I did not send the piece of code of the server, just part for the client.

    I would appreciate your help.

    Reception slot

    void WinClient::MessageRecieved()
    {
    QString messageRecived;
    QDataStream in(m_socket);
    if(sizeMessage == 0)
    {
    if(m_socket->bytesAvailable() < (int)sizeof(quint16))
    {
    return;
    }
    in>>sizeMessage;
    }

    if(m_socket->bytesAvailable() < sizeMessage)
    {
    return;
    }

    in >> messageRecieved;

    m_zoneMessage->append(messageRecu);
    sizeMessage = 0;
    }
    Sending slot

    void WinClient::clicOnBoutonConnection()
    {
    m_zoneMessage->append(tr("<em>Attempt of connexion...</em>"));
    m_boutonConnexion->setEnabled(false);
    m_socket->abort();
    m_socket->connectToHost(m_textIp->text(), m_ServerPort->value());
    }
    Many thanks in advance

  6. #6
    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: Transfert of File with QTcpSocket

    Quote Originally Posted by Stanfillirenfro View Post
    This piece of code should open a directory, read a file, copy it and sent it to different cilents.
    As already stated it does not do that. It wrecks the file by overwriting the first bytes and then dumps that into the socket.

    I'm not sure how you wish to differentiate between a "chat message" and a "file" on the receiving end if you are not using any kind of protocol for communication but instead you rely on the broken fortune cookie example without even understanding how it works.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Transfert of File with QTcpSocket

    Thanks Wysota for your explaination.

    Since I am new on this point, could you please give me a path how to fix the problem? To my knowledge, the server should receive and send the "chat message" in the same way like a file, the only difference being th esize of each. PLease corect me if I am not correct.

    Thanks in advance

  8. #8
    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: Transfert of File with QTcpSocket

    Quote Originally Posted by Stanfillirenfro View Post
    Since I am new on this point, could you please give me a path how to fix the problem?
    The path is to design a communication protocol first before you start network programming.

    To my knowledge, the server should receive and send the "chat message" in the same way like a file, the only difference being th esize of each. PLease corect me if I am not correct.
    How should I know? It's your server not mine. If you claim the receiving end should be the same then I'll say that the sending end should be the same as well. However I have no idea how you wish to display an image file as text in the chat window.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. sending a file(txt,docs,exe,rar,zip...) through QTcpSocket
    By toufic.dbouk in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2012, 17:03
  2. QTcpSocket problem in file transfer
    By omprakash in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2010, 08:18
  3. File Transfer with QTcpServer and QTcpSocket
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2009, 17:12
  4. Best way to check if file is successfully sent over QTcpSocket
    By cutie.monkey in forum Qt Programming
    Replies: 5
    Last Post: 17th October 2009, 20:24
  5. Replies: 3
    Last Post: 29th June 2007, 08:32

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.