PDA

View Full Version : Sending mp3 files over QTcpSocket



ada10
16th August 2010, 12:39
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 -


QFile htmlfile("c:\\server_files\\ywr.mp3");
if (!htmlfile.open(QIODevice::ReadOnly))
return;

QTextStream os( clientSocket );
//os.setAutoDetectUnicode(true);
os << "HTTP/1.0 200 Ok\r\n"
"Content-Type: audio/mpeg; charset=\"utf-8\"\r\n"
"\r\n";
os.flush();

// Streaming the file
QByteArray block = htmlfile.readAll();
clientSocket->write(block);

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 ?

squidge
16th August 2010, 13:43
It seeems you misunderstand the mime format.

ada10
17th August 2010, 04:50
I could not get the reason why u told that ? Can u explain a bit more ?

aamer4yu
17th August 2010, 05:41
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.

tbscope
17th August 2010, 05:53
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.

squidge
17th August 2010, 08:05
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.