I have a server-client application that I'm working on that basically simulates a chat room. This is an assignment for school and the protocol specifications are somewhat strict.
I have a char array which will store all messages from a client.
The client must first send the length of the message as a uint8_t and then the message itself as a char array.
My problem is I need to store the uint8_t value that is sent before the actual message is sent but I can only use the message array to store any information coming from the client.
If I'm not mistaken the char array will not store the uint8_t that gets sent over unless I cast it somehow.
How can I convert the uint8_t to characters and back to uint8_t?
I've tried looking for a similar problem on here but couldn't find an example.
server.c
char msg[100];
recv(clients_sd, msg, sizeof(msg), 0);
uint8_t len; /* store the length of the message here */
char message_received[len];
recv(clients_sd, message_received, sizeof(message_received), 0); /* get and store the message here */
client.c
uint8_t length = 21;
char clients_message[] = "Hi how are you today?";
send(servers_sd, &length, sizeof(length), 0);
send(serers_sd, &clients_message, sizeof(clients_message), 0);
If you're on an architecture where uint8_t
is a typedef
to unsigned char
(you most likely are), then simply take the first char
and cast it to uint8_t
:
length = (uint8_t)(message_received[0]);
It should work.