I'm sending bytes through sockets and that works fine. Now in terms of transfer files, I need to send file size before sending actual data. So I come up a idea that send file size as a header before file data.
For example, if file data is "1234", then socket will send 4 bytes of its length (which is 4) before "1234" and there will be 8 bytes sending through socket. First 4 bytes contains data length, and the rest of bytes are actual data.
I can convert int into bytes (i.e. char array) by:
bytes[0] = (n >> 24) & 0xFF;
bytes[1] = (n >> 16) & 0xFF;
bytes[2] = (n >> 8) & 0xFF;
bytes[3] = n & 0xFF;
The problem is I can't add this 4 bytes before actual data:
char *data = [Actual data of file];
char *buffer = malloc(4 + strlen(data));
strcpy(buffer, bytes);
strcat(buffer, data);
The result of this is that bytes[] are gone, and buffer has exactly same data as data
Can anyone help me with this?