Jump to content
  • Advertisement
Sign in to follow this  
Jarry

Sending a file to an FTP

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

Hello all, I'm wondering how to send a file as fast as possible to an FTP. I am sending line by line at the moment, but I don't think it is the faster way because it can take up to 10 seconds, when my FTP programm seems to goes twice faster. I am sending .txt, .html and .php file. The size of the .txt can expand infinitely. Now lets see the code:
//OPENING A FILE AND READING LINE BY LINE
void sendfile(const string& filename)
{
	ifstream myfile(filename.c_str());
	if (myfile) {

		string line;
		while (getline(myfile, line)) {
			sendby(line, ftpsend); //SEND ONE LINE
		}

	}
}


//SENDING A LINE
void sendby(const string& e, const int& socketWasActive)
{
	unsigned size_sent = 0;
	unsigned total_size = (unsigned) e.size();
	while (size_sent < total_size)
		size_sent += send(socketWasActive, e.c_str() + size_sent, (unsigned) e.size() + size_sent, 0);

	cout << e << endl;
}


Thanks for any kind of help.

Share this post


Link to post
Share on other sites
Advertisement
Treat the file as binary. Read the entire thing into memory, and call send() on the entire data, walking the pointer forward each time it returns until you reach the end. This means you don't have to wait for the disk between each send().

Share this post


Link to post
Share on other sites
Any advice on the format I should use please ?! Would a char array be fine to hold a whole 1 mb text file please ?

[Edited by - Jarry on February 14, 2007 12:21:45 PM]

Share this post


Link to post
Share on other sites
Ok I did that:


void sendfile(const string& filename, const int& ftpsend)
{
ifstream myfile(filename.c_str(), ios::binary | ios::ate);
if (myfile) {
ifstream::pos_type size = myfile.tellg();
char *file = new char [size];

//STORING THE FILE
myfile.seekg(0, ios::beg);
myfile.read(file, size);

myfile.close();

sendby(file, ftpsend);
delete[] file;
}
}


void sendby(const char * e, const int& socketWasActive)
{
int size_sent = 0;
int total_size = (int) strlen(e);
while (size_sent < total_size)
size_sent += send(socketWasActive, e + size_sent, (int) strlen(e + size_sent), 0);

cout << e << endl;
}






The problem is that the function adds weird chars: "ýýýý««««««««îþîþ" to the file. Might be because the char pointer is too big, and the null char appends too late. Any idea please ? Otherwise is the function good please ?

[Edited by - Jarry on February 14, 2007 1:33:06 PM]

Share this post


Link to post
Share on other sites
sendby should take the data pointer and the size, rather than trying to measure the length using strlen. If you ever want to send data containing 0, sendby wouldn't send the right amount otherwise.

Share this post


Link to post
Share on other sites
I'm not sure, sorry, if I have to remove both strlen or only the first one. So far I have removed the first one, but still it seems it adds extra char when sending, must be due to the file pointer.


void sendby(const char * file, const int& socket, const int& size)
{
int size_sent = 0;
while (size_sent < size)
size_sent += send(socket, file + size_sent, (int) strlen(file + size_sent), 0);

cout << file << endl;
}

void sendfile(const string& filename, const int& ftpsend)
{
ifstream myfile(filename.c_str(), ios::binary | ios::in | ios::ate);
if (myfile) {
ifstream::pos_type size = myfile.tellg();
char *file = new char [size];

//STORING THE FILE
myfile.seekg(0, ios::beg);
myfile.read(file, size);

myfile.close();

sendby(file, ftpsend, size);
delete[] file;
}
}




I have no idea how to do if I must not use strlen in the send() function tough, and I have no idea how to remove those unwanted chars.

Thank you.

Share this post


Link to post
Share on other sites
Wouldn't you need to do something like this?

char *file = new char [size+1];

Then I would initialize the 'file' buffer to zero.

As the file you are trying to send might not finish with a zero.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!