Advertisement

Sending a binary file over TCP from server to clients

Started by December 04, 2002 03:14 PM
2 comments, last by trondb 22 years, 2 months ago
I have a client server program, that I am trying to extent to a Internet-Radio program with multiple clients... How do I send a binary file (WAV in my case) from the server to the client?!?!? For starters I just want to read the file from disk on the server side, send it over to the client, and write it to disk there... Thanks alot, I really need help on this one! "That''s Tron. He fights for the users."
"That's Tron. He fights for the users."
Nothing too strange with doing that - just write the file contents into the send buffer and transmit, as much as the buffer will hold at a time. Reassemble on the client side. You''ll need to make sure the client knows how much data to expect and what to do with it.

BUT

I would seriously suggest you rethink sending WAVs to multiple clients over TCP, thats two pretty bad design decisions (or requirements) if I may say so.

WAVs are huge. You don''t want to send those, you want some sort of compressed format if at all possible, mp3 or ogg for instance.

Duplicating each file via TCP to each client is also wasteful, both for bandwidth and server resources. Make a streamed UDP implementation instead. If your target architecture allows it, look into multicasting too. If you are transmitting over the internet, make the application able to skip lost parts of the file (it will sound better than the silence while waiting for TCP to fill all gaps, anyway)
Advertisement
Sounds good,
BUT I have a problem however...
--------
void loadWave(){
char *audioBuffer;
const char* filename = "radio.wav";
long size;
ifstream file (filename, ios::in|ios::binary|ios::ate);
printf("Buffering %s...",filename);
size = file.tellg();
printf("(%d bytes)",size);
file.seekg (0, ios::beg);
audioBuffer = new char[size];
file.read(audioBuffer, size);
file.close();
printf("%d bytes ",strlen(audioBuffer)); printf("done\n");
}
If I use the radio.wav file, i get only 7 bytes as the strlen of audioBuffer, while the filesize ( size = file.tellg() ) is actually 424644 bytes.

And if I try with a textfile.txt instead it gives me 515 instead of 502....

Any ideas what I am doing wrong here?!?!?
(I will probably switch to MP3 as soon as I get the basics going )

"That''s Tron. He fights for the users."
"That's Tron. He fights for the users."
Yes, you are making a very common mistake. The strlen() function cannot be used for binary buffers, it expects a null-terminated string. You cannot use strlen to determine how many bytes of data you have read from the file or received over a socket.

[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com

This topic is closed to new replies.

Advertisement