Threads, sockets and TCP

Thread Solved

Join Date: Apr 2008
Posts: 13
Reputation: LightSystem is an unknown quantity at this point 
Solved Threads: 0
LightSystem LightSystem is offline Offline
Newbie Poster

Threads, sockets and TCP

 
0
  #1
Nov 28th, 2008
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:

  1. void *handle_client (void *socket){
  2.  
  3. struct message_t *message;
  4. int novoSock = *(int *) socket;
  5.  
  6. while(1){
  7.  
  8. message = malloc(sizeof(struct message_t));
  9.  
  10. message = network_to_message(novoSock);
  11.  
  12. if(invoke(message) == 0){
  13. message_to_network(message, novoSock);
  14. close(novoSock);
  15. }
  16. else{
  17. printf("invoke failure\n");
  18. close(novoSock);
  19. }
  20.  
  21. free(message);
  22. }
  23.  
  24. return NULL;
  25. }

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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Threads, sockets and TCP

 
0
  #2
Nov 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: LightSystem is an unknown quantity at this point 
Solved Threads: 0
LightSystem LightSystem is offline Offline
Newbie Poster

Re: Threads, sockets and TCP

 
0
  #3
Nov 29th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC