Results 1 to 4 of 4

Thread: binary file sending using qt4.3

  1. #1
    Join Date
    Jul 2007
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default binary file sending using qt4.3

    dear friends,
    i am using qt 4.3 for my tcp socket programming.
    in my program i am able to send normal text file from one client application to server application.
    But i am not able to send the binary file or exe file from client application to server application..Plz if any body is having the sample code plz share with me...

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: binary file sending using qt4.3

    Are you opening the file as TEXT file ??
    can we see the code ?

  3. #3
    Join Date
    Jul 2007
    Posts
    20
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: binary file sending using qt4.3

    Dear friend this is my sending side code for text file transfer..

    Qt Code:
    1. void client::sendFile()
    2. {
    3. QFile file("new.txt");
    4. if(!(file.open(QIODevice::ReadOnly)))
    5. QMessageBox::information(this, tr("File"),tr("File cannot be opened."));
    6.  
    7.  
    8.  
    9. QDataStream out(&block, QIODevice::WriteOnly);
    10. out.setVersion(QDataStream::Qt_4_0);
    11. out << (quint16)0;
    12.  
    13. while (!file.atEnd())
    14. {
    15. QByteArray line = file.readLine();
    16. QString om(line);
    17.  
    18. out<<om;
    19.  
    20. out.device()->seek(0);
    21. out << (quint16)(block.size() - sizeof(quint16));
    22. tcpclient->write(block);
    23.  
    24. }
    25.  
    26. file.close();
    27. }
    To copy to clipboard, switch view to plain text mode 

    Then this is my receive side code for text file


    Qt Code:
    1. void server::readClient()
    2. {
    3. QDataStream in(clientConnection);
    4. in.setVersion(QDataStream::Qt_4_0);
    5.  
    6. QFile file("new_file.txt");
    7. if(!(file.open(QIODevice::Append)))
    8. QMessageBox::information(this, tr("File"),tr("File cannot be opened."));
    9. QTextStream txtStrmForFile(&file);
    10.  
    11.  
    12. forever
    13. {
    14. if (blockSize == 0)
    15. {
    16. if (clientConnection->bytesAvailable() < (int)sizeof(quint16))
    17. return;
    18.  
    19. in >> blockSize;
    20. }
    21.  
    22.  
    23.  
    24. if (clientConnection->bytesAvailable() < blockSize)
    25. return;
    26.  
    27. QString strData;
    28. in>>strData;
    29.  
    30.  
    31. txtStrmForFile<<strData;
    32.  
    33. strData="";
    34. blockSize=0;
    35. }
    36.  
    37. file.close();
    38. }
    To copy to clipboard, switch view to plain text mode 

    This works fine as far as new.txt is a text file..but for other files e.g. for any other file this is not working..plz anybody can modify this code and suggest what to be done ..This code is in Qt4.3.3.
    bye
    Last edited by jpn; 22nd May 2008 at 20:59. Reason: missing [code] tags

  4. #4
    Join Date
    May 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: binary file sending using qt4.3

    I suppose the problem is here:
    Qt Code:
    1. QByteArray line = file.readLine();
    2. QString om(line);
    To copy to clipboard, switch view to plain text mode 
    Using readLine doesn't make sense with binary files. And there is no need to convert binary data to unicode.

Similar Threads

  1. sending Text or binary file over network
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2008, 00:42
  2. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10
  5. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 09:23

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.