File transfer through winsock

This is a discussion on File transfer through winsock within the C++ Programming forums, part of the General Programming Boards category; Here is what I got so far: Code: int sendFile(char *filename, SOCKET socket) { long size = getFileSize(filename); FILE *file ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    100

    File transfer through winsock

    Here is what I got so far:

    Code:
    int sendFile(char *filename, SOCKET socket) {
    	long size = getFileSize(filename);
    
    	FILE *file = fopen(filename, "r");
    
    	char* tmp;
    	sprintf(tmp, "%i", size);
    	sendCommand(socket, tmp);
    
    	for(int i = 0; i <= size; i++) {
    		char buffer;
    		fread(&buffer, 1, 1, file);
    		send(socket, &buffer, 1, 0);
    	} 
    
    	fclose(file);
    	return 0;
    }
    
    void recvFile(SOCKET socket) {
    	FILE *file = fopen("moo.txt", "r");
    
    	char* loop = recvResponse(socket); // DATA RECV
    
    	for(int i = 0; i <= stringToInt(loop); i++) {
    		char buffer;
    		recv(socket, &buffer, 1, 0);
    		fprintf(file, &buffer);
    	} 
    
    	fclose(file);
    }
    But it doesn't work, I get a wierd runtime error

    any suggestions?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    char* tmp;
    sprintf(tmp, "%i", size);
    tmp isn't allocated any memory.

    > fprintf(file, &buffer);
    If you only have a single char, use fputc
    fprintf() expects a \0 terminated string for the 2nd parameter
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Special note: that data probably won't go anywhere if it's bigger than 4k.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    100
    Code:
    int sendFile(char *filename, SOCKET socket) {
    	long size = getFileSize(filename);
    
    	if(size == -1 || size == 0) {
    		//sendCommand(socket, "111");
    		return -1;
    	}
    
    	FILE *file = fopen(filename, "r");
    
    	char tmp[1024];
    	sprintf(tmp, "%i", size);
    	//sendCommand(socket, tmp);
    
    	char* buffer;
    	buffer = new char[size];
    
    	fread(&buffer, sizeof(buffer), 2048, file);
    	send(socket, buffer, size, 0);
    
    	printf(buffer);
    	
    
    	fclose(file);
    	return 0;
    }

    Here I am just seeing if i can read the files contents and print them out on the client first.... but every time I do this, i get wierd results (odd ansii characters) and i get different results every time...

    The file im trying to read is not binary

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > fread(&buffer, sizeof(buffer), 2048, file);
    buffer is a pointer, so &buffer is a pointer to the pointer, not where it is pointing

    size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
    The size is the size of each element - in this case, its a char

    You should say size, not 2048 (or some other number)

    Say
    fread(buffer, sizeof(buffer[0]), size, file);

    > send(socket, buffer, size, 0);
    Use the return result of send(). If you try and send too much data, it will return with the amount of data actually sent.
    Code:
    int sent = 0;
    while ( sent < size ) {
      sent += send( socket, &buffer[sent], size-sent, 0 ); /* add error checks */
    }
    This will walk the length of the buffer, sending each block as it goes.
    In general, you have to do this even if your buffer is small.

    > printf(buffer);
    NEVER pass a non-constant string to printf as the first parameter. If there's ever a % character in there, then you lose.
    In addition, since you used fread() to get this, there is no \0 at the end to mark the end of the string.

    > The file im trying to read is not binary
    Code:
    while ( fgets( buff, BUFSIZ, file ) != NULL ) {
      send( socket, buff, strlen(buff), 0 );
    }
    Something like this perhaps?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    100
    wow... thanks for the great reply!

    I got it working, with just one bug...

    Code:
    void sendFileSize(char *filename, SOCKET socket) {
    	char buffer2[256];
    	sprintf(buffer2, "%i", getFileSize(filename));
    	sendCommand(socket, buffer2);  // DATA SEND
    }
    
    int sendFile(char *filename, SOCKET socket) {
    	long size = getFileSize(filename);
    
    	if(size == -1 || size == 0) {
    		//sendCommand(socket, "111");
    		return -1;
    	}
    
    	FILE *file = fopen(filename, "r");
    
    	char tmp[1024];
    	sprintf(tmp, "%i", size);
    	sendCommand(socket, tmp);
    
    	char* buffer;
    	buffer = new char[size];
    
    	fread(buffer, sizeof(buffer[0]), size, file);
    	send(socket, buffer, size, 0);
    
    	printf("%s", buffer);
    
    	fclose(file);
    	return 0;
    }
    
    void recvFile(char *filename, SOCKET socket) {
    	FILE *file = fopen(filename, "w");
    
    	char* csize = recvResponse(socket); // DATA RECV
    	int size = stringToInt(csize);
    
    	char* buffer;
    	buffer = new char[size];
    
    	recv(socket, buffer, size, 0);
    	fprintf(file, buffer);
    	 
    	fclose(file);
    }
    I transfers the ANSII file just fine, but when i compare the oringinal file with the one that was downloaded, the one that was downloaded has some extra characters at the end.

    Also, to transfer in binary, would it work just to change "r" and "w" to "rb" and "wb"?

    Any ideas?

    Thanks a lot!
    Last edited by elfjuice; 05-14-2005 at 02:35 AM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > fprintf(file, buffer);
    Use fwrite to write a fixed amount of data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    100
    Thank you so much! it works great! One question, how can i transfer binary files?

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    100
    Nevermind about the binary question, I answered it myself in oen of my own posts, lol... thanks a lot man!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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