send big chunk of data over linux sockets

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).

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.

Leave a Reply

Your email address will not be published. Required fields are marked *