C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-13-2009, 01:16 AM   #1
Registered User
 
Join Date: Feb 2009
Posts: 2
File Writing Problem

So I'm writing a file transfer routine for two networked machines. I have a working version that has one machine read in the entire file into a buffer, and then transfer chunks over the network. However, I'd like to read in chunks from the file and send them as I go in case the file is very large. With that version, the file is transferred, it has the correct size, but no matter what file type it is, it will not open. For example, a .jpg throws an error saying "DNL not supported" or a text file has improper encoding.

The code for the two version is very similar and I'm unable to figure out what the problem is. I believe the sendFile() function is where the problem is seeing as that is the function that differs in the two versions.

Any help would be appreciated!

Here is the working version that loads the entire file into memory:
Code:
void receiveFile(int sock, char* filename)
{
	FILE * pFile;
	pFile = fopen(filename, "wb");

	message msg;
	void* buffer = (void*) &msg;
	int rBytes, rv;
	int pause;
	
	int finished = 0;
	int bytesWritten = 0;
	
	while(!finished)
	{
		//ensure that we receive a complete msg object from the other end
		for(rBytes = 0; rBytes < sizeof(msg); rBytes += rv)
		{
			if((rv = recv(sock, buffer+rBytes, sizeof(msg)-rBytes, 0)) < 0)
				dieWithError("recv() failed");
		}
		
		//convert size from network to host byte order
		msg.size = ntohl(msg.size);
		
		//write the data we just received to the file
		if(msg.size - bytesWritten < sizeof(msg.data))
		{
			fwrite(msg.data,1,msg.size - bytesWritten,pFile);
			finished = 1;
		}
		else
		{
			fwrite(msg.data, 1, sizeof(msg.data), pFile);
			bytesWritten += sizeof(msg.data);
		} 
	}
	//close file
	fclose (pFile);
}

void sendFile(int sock, char* filename)
{
	FILE* pFile;
	long fileSize;
	char* buffer;
	size_t result;
	
	int copied;

	pFile = fopen(filename,"rb");
	if(pFile==NULL)
		printf("error with fopen()");
		
	// obtain file size:
	fseek(pFile, 0, SEEK_END);
	fileSize = ftell(pFile);
	fseek(pFile, 0, SEEK_SET);

	// allocate memory to contain the whole file:
	buffer = (char*)malloc(sizeof(char) * fileSize);
	if(buffer == NULL)
		printf("error with malloc()");

	// copy the file into the buffer:
	result = fread(buffer, 1, fileSize,pFile);
	if(result != fileSize)
		printf("error with fread()");

	//the whole file is now loaded in the memory buffer.
	
	//send file to other client
	message msg;
	
	copied = 0;
	while(copied < fileSize)
	{
		memset(&msg, 0, sizeof(msg));
		if(fileSize - copied < sizeof(msg.data))
		{
			memcpy(msg.data, buffer + copied, fileSize - copied);
			copied += fileSize - copied;
		}
		else
		{
			memcpy(msg.data, buffer + copied, sizeof(msg.data));
			copied += sizeof(msg.data);
		} 
		msg.size = htonl(fileSize);
		send(sock, &msg, sizeof(msg), 0);
	}

	//close file
	fclose(pFile);
	free(buffer);
}
Here is the one that doesn't work. It uses the same corresponding receiveFile() function.
Code:
void sendFile(int sock, char* filename)
{
	FILE* pFile;
	long fileSize;
	char* buffer;
	size_t readIn;
	
	int copied;

	pFile = fopen(filename,"rb");
	if(pFile==NULL)
		printf("error with fopen()");
		
	// obtain file size:
	fseek(pFile, 0, SEEK_END);
	fileSize = ftell(pFile);
	fseek(pFile, 0, SEEK_SET);
	
	//send file to other client
	message msg;
	copied = 0;
	
	while(copied < fileSize)
	{
		//allocate a buffer of appropriate size
		if(fileSize - copied < sizeof(msg.data))
			buffer = (char*)malloc((fileSize - copied) * sizeof(char));
		else
			buffer = (char*)malloc(sizeof(msg.data) * sizeof(char));
		
		//read in some data from the file
		readIn = fread(buffer, 1, sizeof(buffer), pFile);
		if(readIn != sizeof(buffer))
			printf("error with fread()");
		
		//transfer the data into our message struct
		memset(&msg, 0, sizeof(msg));
		memcpy(msg.data, buffer, sizeof(buffer));
		copied += sizeof(buffer);

		msg.size = htonl(fileSize);
		send(sock, &msg, sizeof(msg), 0);
		free(buffer);
	}

	//close file
	fclose(pFile);
}
polskash is offline   Reply With Quote
Old 02-13-2009, 02:27 AM   #2
cas
Registered User
 
Join Date: Sep 2007
Posts: 372
In your second sendFile() function, you're using sizeof(buffer), but that doesn't mean what it appears that you think it means.

sizeof(buffer) tells you the size of the pointer, not the size of the allocated data: you have to remember that size yourself. Due to this, you're trying to read (probably) 4 bytes each time. Thus if the file size is not a multiple of 4 (or whatever the size of a pointer is), I would expect you to see the "error with fread()" message. Do you ever?

At the very least, change the code to remove the use of sizeof(buffer); you can use a variable to store the amount of memory you allocate and use that instead. If I'm reading the code right, this should fix your problem. In receiveFile() you're assuming each chunk (save the last) is sizeof(msg.data) bytes long, and of course you're actually sending different-sized chunks.
cas is offline   Reply With Quote
Old 02-13-2009, 10:08 AM   #3
Registered User
 
Join Date: Feb 2009
Posts: 2
That was it. Thanks a bunch!
polskash is offline   Reply With Quote
Old 02-13-2009, 10:47 AM   #4
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> if((rv = recv(sock, buffer+rBytes, sizeof(msg)-rBytes, 0)) < 0)
You should also use an unsigned char* for your buffer type.
Relying on being able to add to a void* is not allowed.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Creating File for File Processing Dampecram C Programming 2 12-07-2008 01:26 AM
Problem with file writing Goldrak C++ Programming 7 04-09-2006 06:46 AM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM
Unknown Memory Leak in Init() Function CodeHacker Windows Programming 3 07-09-2004 09:54 AM
Need a suggestion on a school project.. Screwz Luse C Programming 5 11-27-2001 02:58 AM


All times are GMT -6. The time now is 12:00 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22