954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Threads, sockets and TCP

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?

LightSystem
Newbie Poster
24 posts since Apr 2008
Reputation Points: 10
Solved Threads: 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.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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

LightSystem
Newbie Poster
24 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You