How to send to server 4 bytes int and on server side conver this buffer to int.
client side:
void send_my_id()
{
int my_id = 1233;
char data_to_send[4];
// how to convert my_id to data_send?
send(sock, (const char*)data_to_send, 4, 0);
}
server side:
void receive_id()
{
int client_id;
char buffer[4];
recv(client_sock, buffer, 4, 0);
// how to conver buffer to client_id? it must be 1233;
}
You may simply cast the address of your
inttochar*and pass it tosend/recv. Note the use ofhtonlandntohlto deal with endianness.Note: I preserved the lack of result-checking. In reality, you’ll need extra code to ensure that both send and recv transfer all of the required bytes.