Sending files

Started by
2 comments, last by WizHarDx 22 years, 4 months ago
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
who is it that keeps on nicking WizHarD name !! :P
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

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.
CorsairK8@Fnemesis.comLinux Debian/GNU RulezThis is my signitory!C Is Tha Best!
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..
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan

This topic is closed to new replies.

Advertisement