0

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?

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by LightSystem
0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.