I am in the process of making a program to send large files.
This is a Win32 program.
As you can see from my code, when the user clicks the send button, it starts the sending loop, which sends 1kb at a time.
A log file is also opened and written to with the following details:
the amount of data read from the file, and the amout of data sent.
after a while, the amount of data sent will be -1 because send() returns -1.
How do I get past this? Am I even using blocking sockets? How do I implement a blocking socket?
[IMPORTANT - READ]
I realize there could be more data left over to send at the end of the sending loop. That problem is solved with a simple modulous calculation. This is not my problem.
I know sending 1kb at a time is probably not the best. But I know you CAN send 1kb at a time, and this is what I want to do at the moment (I'm learning sockets).
[/IMPORTANT - READ]
Thank you for your replies.
Here is my sending code (executes when a button on the form is clicked)
Code:
case IDC_CLICK_SEND:
{
FILE *fRD = NULL;
long fLength = -1;
char chrRead[1024] = {0};
char outputlog[256];
int actualRead, actualSent;
FILE *LOG = fopen("c:\\documents and settings\\kj\\desktop\\log.txt", "w");
if((fRD = fopen("c:\\documents and settings\\kj\\desktop\\Ballad.mp3", "rb")) == NULL)
{
MessageBox(hWnd, "fopen failed.", "error", MB_OK);
return 0;
}
fLength = retFileLength(fRD);
for(int x = 0; x < (fLength / 1024); x++)
{
if(!fseek(fRD, (x * 1024), SEEK_SET))
{
actualRead = fread(chrRead, sizeof(char), 1024, fRD);
actualSent = send(sckTransfer, chrRead, 1024, 0);
sprintf(outputlog, "Actual Read: %d; Actual Sent: %d.\n", actualRead, actualSent);
fwrite(outputlog, sizeof(char), strlen(outputlog), LOG);
}
}
MessageBox(hWnd, "Done Sending", "", MB_OK);
closesocket(sckListen);
closesocket(sckTransfer);
fclose(fRD);
fclose(LOG);
return TRUE;
}
Here is my receiving code (executes when FD_READ message is received (WSAAsyncSelect)):
Code:
case FD_READ:
{
char chrRead[1024] = {0};
int actualReceived = 0;
actualReceived = recv(sckTransfer, chrRead, sizeof(chrRead), 0);
fwrite(chrRead, sizeof(char), actualReceived, fWR);
return TRUE;
}