I currently have a basic client/server setup. The server needs to take requests from client(s) and need to respond to different request message types. An example of client request could be get list of files available, how many other clients are connected to the server, etc.
I would obviously have to figure out a way to determine the type of message from the message received from the sender. I was wondering, if I have a struct defined with necessary data, whehter if I can cast the struct to void*, send it as through send(sockfd, message, length, flags) syscall, and than on the receiver side, cast it back to the struct. This of course assumes that I am running the client and server in a same environment.
For example, if I have the following struct:
struct message {
enum messageType { GET_FILES, GET_CLINET_NUMBER} messageType,
}
and use this struct to send the message in following way
struct message msg;
msg.messageType = GET_FILES;
send(server_sockfd, (void*)&msg, sizeof(struct), 0)
and on the receiver,
recv(,msg_buffer);
struct message received = (struct message*)msg_buffer;
Ignoring the minor syntactical issues, can anyone advise if this scheme is possible? If not, is there any other way to pass a message without sending raw char*?