Results 1 to 6 of 6

Thread: Sending mp3 files over QTcpSocket

  1. #1
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Exclamation Sending mp3 files over QTcpSocket

    I am trying write a server application which reads an mp3 file on the file system & sends it to a client app through QTcpSocket using the following code -

    Qt Code:
    1. QFile htmlfile("c:\\server_files\\ywr.mp3");
    2. if (!htmlfile.open(QIODevice::ReadOnly))
    3. return;
    4.  
    5. QTextStream os( clientSocket );
    6. //os.setAutoDetectUnicode(true);
    7. os << "HTTP/1.0 200 Ok\r\n"
    8. "Content-Type: audio/mpeg; charset=\"utf-8\"\r\n"
    9. "\r\n";
    10. os.flush();
    11.  
    12. // Streaming the file
    13. QByteArray block = htmlfile.readAll();
    14. clientSocket->write(block);
    To copy to clipboard, switch view to plain text mode 

    The server application crashes when this code is executed. What is the problem in the above code ? Is the method of reading the mp3 file faulty ?

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sending mp3 files over QTcpSocket

    It seeems you misunderstand the mime format.

  3. #3
    Join Date
    May 2010
    Posts
    46
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Exclamation Re: Sending mp3 files over QTcpSocket

    I could not get the reason why u told that ? Can u explain a bit more ?

  4. #4
    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: Sending mp3 files over QTcpSocket

    What is the end client you suppose will be reading your mp3 file ??
    I dont think streaming means simply writing the file on socket. Thats file transfer, not streaming.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Sending mp3 files over QTcpSocket

    Quote Originally Posted by ada10 View Post
    The server application crashes when this code is executed. What is the problem in the above code ?
    You need to post the complete code to know where and why it crashes. Your code snippet contains variables that are defined and created outside your snippet.

    Another problem is that mp3's are binary. Are you sure that the way you send the data is correct? You can't send binary data as text for the simple reason that most, if not all, clients will parse text as text. So, if you have a \n or \t or \0 character in your binary, the client will corrupt the binary. Let alone the classes and functions you use to send the data which may corrupt the data already at your end.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Sending mp3 files over QTcpSocket

    Why are you trying to emulate a web server? Doing so requires much more work than what you used above. For example, you must parse the client request correctly before responding. Use a ethernet spy tool to see how a real web browser/server works.

Similar Threads

  1. Streaming files to web browser using QTcpSocket
    By braaja in forum Qt Programming
    Replies: 3
    Last Post: 29th July 2014, 20:30
  2. Packet getting fragmented when sending over QTcpSocket
    By arturo182 in forum Qt Programming
    Replies: 14
    Last Post: 5th August 2009, 23:11
  3. Replies: 12
    Last Post: 17th June 2009, 05:34
  4. Sending Mail with attachment files
    By joy in forum Qt Programming
    Replies: 2
    Last Post: 14th March 2008, 08:51
  5. Compress and Uncompress large files in QTcpSocket
    By vishesh in forum Qt Programming
    Replies: 4
    Last Post: 26th June 2007, 22:21

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.