Advertisement

Winsock sending and recieving files!

Started by July 17, 2004 02:05 PM
3 comments, last by franky 20 years, 7 months ago
Hello :) I created a small winsock client/server chat some time ago.. Now I'm trying to create a "fast and stable" file sending client/server application. I’m having trouble with the sending and receiving part.. The application use only one socket, but both the server and client should be able to send a file if the socket is connected but not in use. I know how to set up the socket and how to connect, and regarding the file sending it works fine with smaller files, but when I tried to send a 30mb file I found out that I probably need to divide the file into smaller chunks. I have managed to load my file into a buffer (char* buffer). But I don't know how I should set up the loop for splitting and sending the file.. And I don’t know how to set up my receiving structure in FD_READ.. All help is very appreciated :) Thank you!
Maybe you should show specific lines of code for what you are doing and where you're having troubles? From what you've said, considering how far you got, it doesn't seem like you should have any major troubles finishing it out.

You might want to check out the Asynchronous server example from sockaddr.com, which was written for the book "Winsock 2" by Lewis Napper. IIRC, it demonstrates splitting a file over multiple send calls.
Advertisement
Thank you for your reply SiCrane!

My code is still a bit messy but I will try to write down some lines. To make it a bit easier to explain lets say that both the server and the client know the exact size of the file to be transfered. And I want to send my chunks from the client and let the server receive, and patch the file back together..


#define FILESIZE 26311722


//Client
My "cltSocket" socket is connected. I have opened the file "alig.mpg" (approximately 25mb) for binary reading and I have transferred this file into my buffer like this..
buffer = new char[FILESIZE];
fread(buffer,1, FILESIZE,pFile);

In the next step don’t know what to do?? But I guess I want to send the file as 1024, 1500, or 2048 byte chunks..

//char data[1024];
//while(!feof(pFile) )
//{
//size_t num = fread( data, 1, sizeof(data), pFile );
//send( cltSocket, data, num, 0 );
//}


//Server

I open the a file 1.mpg for binary writing when the socket is connected.. I close the file when the socket is disconnected..

I don’t know how to set up my receiving structure??

case FD_READ:
bytes_recv = recv(wParam, buffer, FILESIZE , 0);
fwrite (buffer ,1 ,FILESIZE , pFile);
break;


If you need more code please let me know :)
When sending the file in chunks you should use the return value of the send() function to determine how many bytes were actually sent. You can then use that value to track the total number of bytes sent. The total number of bytes sent can be used to index into the file that you've loaded into memory to determine the address of the next chunk to send.

Of course it might be less memory intensive to read from the file incrementally before you send. (It may complicate things if you have partial inner writes however.)

The reverse can be done in the receiving side. Just keep a buffer for reading the size of your expected chunk size. Then recv into the buffer, and use the numbers of bytes received to determine how much to write to the file.

Did that make any sense?
Yes it shure did SiCrane :)

But It would still be very nice to see two small code examples for the read from and the write to the file incrementally..

Thank you for all your help!

This topic is closed to new replies.

Advertisement