I am learning to do socket programmming and multithreaded programming in c on windows. I have designed a project where there will be three types of nodes for backup(server, client and storage node). I have created the following to have one server and multiple clients and storage nodes. The server needs to create two kinds of threads based on the type of client requesting the service(to be explicit normal client or storage node). I am using a blocking i/o mode. The structure of the code is like this:
Server:
int main()
{
//initialization and other things
while ((new_socket = accept(srv_sock, (struct sockaddr *)&client, &c)) != INVALID_SOCKET)
{
_beginthreadex(0, 0, handle_client, &new_socket, 0, 0);
}
}
uint32_t __stdcall handle_client(void *data)
{
SOCKET* sock = (SOCKET*)data;
SOCKET client_sock = *sock;
//other
recv_size = recv(client_sock, header_buf, HDR_LEN, 0);
//fixed length header
if (!strncmp(connect_with, "storageNode", strlen(connect_with)))
//check if client is a normal client or a storage node
{
_beginthreadex(0, 0, handle_storage_node, sock, 0, 0);
return 0;
}
else
{
//continue with request from normal client
}
}
uint32_t __stdcall handle_storage_node(void *data)
{
SOCKET* sock_SN = (SOCKET*)data;
SOCKET str_node_sock = *sock_SN;
//continue with request from storage node
}
The main reason among other things for me to want to change it into an overlapped i/o is because some times(probably once in a thousand times) a message from a normal client ends up as a message from storage node and vice versa. I think the reason for that is winsock is not strictly thread safe. Plus as a beginner I want to learn to do it in another way. So, what should be the equivalent structure for the overlapped i/o implementation? And how do I stop the messages from being delivered to the wrong thread?
PS:- I am a beginner take it easy on me!