I am having a problem with the following. In particular, I am trying to extract a uint32_t and a char* from the buffer argument passed into the recvfrom() method. At this point, the integer can be extracted properly using the following code:
recvfrom(s, buf, buffer_size, 0, (struct sockaddr*)&si_other, &slen);
uint32_t recv_int = ntohl(*(int*)buf);
char* recv_char = (char*)malloc(6); // NOTE: The original string was "Hello", which has 6 bytes.
strcpy(recv_char, ((char*)buf + sizeof(uint32_t)));
printf("The returned values are %d %s\n", recv_int, recv_char);
However, when I perform printf as shown above, only recv_int has a value. recv_char is a blank string. However, I originally stored "Hello" in the buffer and hence, "Hello" should be printed to stdout.
EDIT:
This is the code that was being used in sendto():
uint32_t my_int = 3;
char* sendString = "Hello";
char* buffer = (char*)malloc(strlen(sendString) + sizeof(int));
memcpy(buffer, &my_int, sizeof(int));
strcpy((char*)buffer + sizeof(int), sendString);
if (sendto(s, buffer, sizeof(int) + strlen(sendString), 0, (struct sockaddr*)&si_other, slen) == -1)
{
printf("Issue with send\n");
}
Any help would be appreciated.