Reputation: 17
I am creating a socket program in c. It divides data from a file into multiple chunks and sends over the socket. The data at destination arrives in out of order fashion. How do I manage this. I cant keep all the data in memory till the end as the file is large.
Upvotes: 0
Views: 1329
Reputation: 27542
Create a sparse file in advance and fill it in as you go along. You didn't specify what OS but here is an example program of how to do it in linux.
#define _BSD_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
{
perror("open");
return(1);
}
int offset = lseek(fd, 200000, SEEK_CUR);
ftruncate(fd, offset);
close(fd);
return 0;
}
Presumably you have some kind of scheme by which you know which chunks (i.e. offsets) are coming in on each socket. Use that to calculate the proper offsets into the file and write them.
Upvotes: 0
Reputation: 4810
I'm not familiar with standard methodology in such a situation, but afaik tcp is ordered. If you're stuck with udp, you could always just prepend a 1 byte (or less) block to each chunk to specify the ordering, then sort them on the client side using that.
Upvotes: 3