I'm writing code for an application using sockets where I have to send integers and strings over the network, but I'm having trouble packing the data into a buffer for transmission. I tried doing this:
sendline[0] = htons(3);
sendline[1] = htons(strlen(argv[3]));
for(int i = 0; i < strlen(argv[3]); i++)
{
sendline[i + 2] = argv[3][i];
}
sendline[2 + strlen(argv[3])] = htons(atoi(argv[4]));
sendline[3 + strlen(argv[3])] = '\0';
but it doesn't work. Where am I going wrong here? Also, what would be the best way to serialize this kind of data?
This is my deserialization code :
case '3': // switching on the value of buf[0]
{
int i;
int len = ntohs(buf[1]);
char* ch = (char*)malloc(len * sizeof(char));
for(i = 0; i < len; i++)
{
ch[i] = buf[i + 2];
}
}