As the topic states, my recv() call keeps stopping when there is 508 bytes left to read. I'm trying to send a file, and it's all going good untill we hit the magical 508 bar.
Sending
void
sendFile(char *fileName)
{
struct fileInfo info;
FILE *file;
long fileSize;
char *buffer, temp[512];
int left, sent, total;
/* Open the file */
file = fopen(fileName, "rb");
if (file == NULL)
logThis("sendFile", "Out of memory while opening file.");
/* Obtain file zie */
fseek(file, 0, SEEK_END);
fileSize = ftell(file);
rewind(file);
/* Allocate memory for the buffer */
buffer = (char *)malloc(sizeof(char) * fileSize);
if (buffer == NULL) {
logThis("sendFile", "Out of memory.");
exit(99);
}
/* Copy file contents over to our buffer */
fread(buffer, 1, fileSize, file);
logThis("sendFile", "Buffer contains: %s", buffer);
/* Send the file name first, and then our size */
info.size = fileSize;
snprintf(info.name, 32, "%s", fileName);
logThis("sendFile", "Name: %s, size: %d", info.name, info.size);
sendCommand(NET_FILEINFO, (char *)&info);
/* Start sending file */
total = 0;
left = fileSize;
logThis("sendFile", "Ready to send: %d bytes", left);
while (total < fileSize) {
sent = send(serverSocket, buffer + total, left, 0);
if (sent == 0) {
logThis("handle", "Connection closed during transfer");
break;
} else if (sent < 0) {
logThis("handle", "Error during tansfer!");
break;
} else {
total += sent;
left -= sent;
logThis("sendFile", "Sent: %d bytes. %d bytes left", sent, left);
}
}
logThis("sendFile", "Done sending. Sent: %d vs %d", total, fileSize);
}
And where i get the file
void
*handle(void *argument)
{
struct fileClient *client;
struct fileInfo *info;
char buffer[512], *fileBuffer;
FILE *file;
int total, read, ret;
/* Get the client */
client = (struct fileClient *)argument;
/* Ready for files, send OK message */
sendCommand(client->clientSocket, NET_OK, " ");
/* Expect file name and then size */
recvCommand(client->clientSocket, NET_FILEINFO, buffer);
info = (struct fileInfo *)buffer;
logThis("handle", "File name: %s file size: %d", info->name, info->size);
/* Allocate memory for the file */
fileBuffer = (char *)malloc(info->size);
/* Keep recieving data and copying it into the file buffer untill done */
total = 0;
read = info->size;
while (read > 0) {
ret = recv(client->clientSocket, (fileBuffer + total), read, 0);
if (ret == 0) {
logThis("handle", "Connection closed during transfer");
break;
} else if (ret < 0) {
logThis("handle", "Error during tansfer!");
break;
} else {
total += ret;
read -= ret;
logThis("handle", "Read: %d bytes. %d bytes left", ret, read);
}
}
/* Create the file on disk and save it */
file = fopen(info->name, "wb");
fwrite(fileBuffer, 1, info->size, file);
fclose(file);
pthread_exit(NULL);
}
I should note that sometimes, the file manages to be downloaded completly.