Jump to content
  • Advertisement

Archived

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

WizHarDx

Sending files

This topic is 6169 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

Hi I am newby on network programming & I was wondering how programs like Napster , Msn Messenger etc send actual files through the network like mp3 s ? thanx in advance WizHarD

Share this post


Link to post
Share on other sites
Advertisement
Message type 1 contains the name of the file to be created and opened. Message type 2 is data to be placed in the file created by message 1.

On the server side you just open a file and read in N characters at a time and send them to the client which writes them back to the file. Part of the message could be the number of bytes to write.

Message type 1 should contain the file name. If more than one file can be sent at once, message type 2 should also contain the name. Just use file.open(filename,ios::app) and close it after the bytes are written with message type 2.

Ben

Share this post


Link to post
Share on other sites
if your using blocking sockets, just do :

SOCKET socket = GetSocket();
FILE *myfile;

myfile = fopen("music.mp3", "rb");
char buf[256];
int bytes;

while (!feof(myfile))
{
bytes = fread(buf, 1, 256, myfile);
send(socket, buf, bytes, 0);
}

The send will take its time to send, if however you are using asynchronous or non-blocking sockets. You will need to wait for a WRITE message, from your select() call or message pump and then try to send as much data as you can each time you receive one. This message will be sent from the operating system as soon as your network transmission layer is capable of sending more data.

Share this post


Link to post
Share on other sites
It''s one of those things where there is no one way to do it, you can choose whichever way suit you.

You could use an industry protocol. For example, I use HTTP when sending files across networks. It is a very good protocol for transferring any kind of file, and the headers can describe all sorts of information about the file. I have been programming HTTP for a while so I have my own classes that handle this, which is why it makes sense for me to use it in all my projects.

Having said that it''s a larger learning curve to implement it properly. You could invent your own protocol that describes what file is going to be sent, how big it is etc. and then send the file. This would give you more control over what information get sent and how each end of the communication parses the information.

I don''t know what your performance implications are but it might be nicer to use an FTP way of doing things where you use two streams; one control stream and one data stream. Opening a TCP socket can be costly timewise and so if you can pipeline data down a permanent connection, instead of opening a connection per transfer, that would be better.

It all depends on your setup really..

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!