Hi. Im working on a project with a relative size and Ive run into a deadlock, I need some advice in how to proceed.
I have a server that creates multiple threads to handle its clients, but each thread must serve a client multiple times,
here is the thread handling function:

void *handle_client (void *socket){

  struct message_t *message;
  int novoSock = *(int *) socket;
		
  while(1){

  message = malloc(sizeof(struct message_t));
	
  message = network_to_message(novoSock);

  if(invoke(message) == 0){
    message_to_network(message, novoSock);
    close(novoSock);
  }
  else{
    printf("invoke failure\n");
    close(novoSock);
  }
	
  free(message);
}
	
   return NULL;		
}

Problem is that after doing the while once I need the thread to wait for a new message to reach the socket,
but how can I do that with a persistent connection? How do I know when to get the message coming from the client? Anyone with any ideas?

message_to_network should already be a function that waits for a new message to reach the socket. I'm guessing about its implementation, but you should just need to call that function -- it will return as soon as the message arrives, or when the connection times out. Obviously, it makes no sense to close the socket.

Yes your right my network_to_message has to handle that, also I didnt notice that I closed the socket on the successful case, thanks for pointing that out. Solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.