0

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!

John_D
  • 29
  • 6
  • 'I think the reason for that is winsock is not strictly thread safe' - I doubt that. I think someone would have noticed by now. Occam suggests that it's hugely more likely that any bug/s are in you own code.. – Martin James Mar 31 '18 at 06:16
  • you not need create any threads on client request. you really need use asynchronous io - `AcceptEx` but not `accept`, `WSARecv` but not `recv`, etc – RbMm Mar 31 '18 at 07:35

1 Answers1

1

Your problem is not Overlapped mode or not. It's that your program acts on invalidated data.

In lines like this

_beginthreadex(0, 0, handle_client, &new_socket, 0, 0);

you are passing the address of a variable on the stack to the new thread. This address will be outside of the while loop iteration. And most likely will be used to store the next socket handle when accept succeeds the next time.

To fix this you could heap allocate each socket instance and pass that function to your worker thread.

Overlapped most will just complicate everything. If you don't know why exactly you need it you there is no reason to use it.

Matthias247
  • 9,836
  • 1
  • 20
  • 29
  • It WILL DEEFINITELY be used to store the next socket handle when accept succeeds the next time. The problem arises when two accept() retuns happen in quick succession: the second accept() loads new_socket before the thread function can copy out the previous value with 'SOCKET str_node_sock = *sock_SN;'. ' If you don't know why exactly you need it you there is no reason to use it' - I agree. If you make such mistakes with threads/pointers, youi will get into trouble with overlapped I/O and/or IOCP. – Martin James Mar 31 '18 at 06:28