Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

trondb

Sending a binary file over TCP from server to clients

This topic is 5736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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."

Share this post


Link to post
Share on other sites
Advertisement
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)

Share this post


Link to post
Share on other sites
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."

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!