No. The point trying to be made in this thread is that if you write socket code like it is meant to be written, then your program won't give a damn about packet sizes. Nor does it need to - let the OS and the network decide what size packets should be - that's their job, so that you don't reinvent the wheel. Don't worry about TCP packet sizes. Your socket, afterall, is a "stream"... no packets, at least from your perspective as a programmer.
Send files of (almost) any size is trivial once you understand this. A very basic approach, is to send the size of the file, then the file itself. The other size receives the size (which it knowns ahead of time is like, 4 bytes) and then recv()'s like crazy. Concept:
Code:
int SendInt(int sock, unsigned int i)
{
char buf[4];
buf[0] = i >> 24 & 0xFF;
buf[1] = i >> 16 & 0xFF;
buf[2] = i >> 8 & 0xFF;
buf[3] = i & 0xFF;
return SendFull(sock, buf, 4);
}
int SendFull(int sock, void *buf, unsigned int size)
{
int sent = 0, send_ret;
while(sent < size)
{
send_ret = send(sock, buf + sent, size - sent, 0);
if(send_ret <= 0) return 1;
sent += send_ret;
}
return 0;
}
int SendFile(int sock, FILE *fp)
{
int file_size, bytes_read, total_read = 0;
char buf[1024];
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
if(SendInt(sock, file_size))
{
fprintf(stderr, "Failed to send file size.\n");
return 1;
}
while(total_read < file_size)
{
bytes_read = fread(buf, sizeof(char), 1024, fp);
if(bytes_read == 0) break;
if(SendFull(sock, buf, bytes_read)) break;
total_read += bytes_read;
}
if(total_read < file_size)
{
fprintf(stderr, "Failed to read all of file.\n");
return 1;
}
return 0;
}
I'll leave it up to you to write a receiving end and find any bugs. And the buffer in there could be half size, double size, the receiving end wouldn't know or care.
SendFull() is the one you should really look at. (Only 13 lines) As long as the socket doesn't explode, it ensures the buffer is sent. Completely. How? It asks send() how much was actually transferred.