Sending a big chunk of data over linux sockets can be tricky, because we need to call the write function multiple times. The following snippet assumes a valid socket filedescriptor “socketfd”, after the connection is established. Make sure that PACKETSIZE is smaller or equal to 2^16 and that nbytes equals the amount of bytes of the data variable (which is a pointer to your data which has to be sent).
int size = min(PACKETSIZE, nbytes-i);
n = write(sockfd, (void*)data+i, size);
if (n < 0)
The typecast to void* is needed to have “i” impacting on the amount of bytes of the data an not of the actual type it is! For the receiving side, the code is quite similar.