Results 1 to 5 of 5
Hi! I am programming a server in C and I have some questions.
In all the examples I have found the server accepts the client's conection, proccess the data received ...
- 11-25-2010 #1Just Joined!
- Join Date
- May 2010
- Location
- Madrid
- Posts
- 21
Server programming with 2 threads per client
Hi! I am programming a server in C and I have some questions.
In all the examples I have found the server accepts the client's conection, proccess the data received and close the socket.
In an very schematic way it would be something like:
But my server has to remain sending data in a loop and attend client's orders when necesary. So I have done two threads, one for receiving and another for sending:Code:client_thread{ select to see if there is data to read from socket fd if there is something to read{ read and store data in a buffer proccess the data received send a reply close connection exit } }
what do you think? is this a good way to do it? I dont have experience in client/server programming and I do not know if there is a better way to do this.Code:// Thread to receive data from client client_thread_in{ while(!exit){ select to see if there is data to read from socket fd if there is something to read{ read and store data in a buffer proccess the data received decide if it has to send something back and update the flag something_to_send } } } // Thread to send data from server to client client_thread_out{ while(!exit){ while(something_to_send){ select to see if the socket fd is ready to write if socket fd is ready{ send data to client } } } }
Should I use mutexs or semaphores to block the socket fd before read and write or it is not necesary?
Thanks!
- 11-25-2010 #2Just Joined!
- Join Date
- Oct 2009
- Posts
- 85
If i understand correctly.. server has two tasks..
1. Send data to someone continusly..
2. Attend client request's.
Spawn 2 threads
Code:SendData() { while(1) { senddata(); } } ClientRq() { while(1) { select(); /*Blocking Select*/ if(client request made) { spawn another thread ClientReqProcessing.. to do the client task } } } ClientReqProcessing() { }Last edited by MikeTbob; 11-25-2010 at 01:15 PM. Reason: Added code tags
- 11-25-2010 #3Just Joined!
- Join Date
- May 2010
- Location
- Madrid
- Posts
- 21
No, it has one listening thread to attend client's connections.
When a client connects to the server two new threads are created, one to receive client's requests and another one to send data to the client. (the aplication will have only one or two clients).
it would be:
The pair of threads created for the connected client:Code:listening_thread{ while(1){ accept(...) when a client connects it creates a pair of new threads (one to receive and one to send data) } }
But I am not sure if this is very.. "academic" : PCode:// Thread to receive data from client client_thread_in{ while(!exit){ select to see if there is data to read from socket fd if there is something to read{ read and store data in a buffer proccess the data received decide if it has to send something back and update the flag something_to_send } } } // Thread to send data from server to client client_thread_out{ while(!exit){ while(something_to_send){ select to see if the socket fd is ready to write if socket fd is ready{ send data to client } } } }
should I use semaphores before read and write the socket fd?
- 11-30-2010 #4Just Joined!
- Join Date
- Nov 2006
- Location
- Harrisburg, PA, USA
- Posts
- 56
I would suggest to have one thread for listen as well as receiving client requests. You know the server listen fd in advance. You can put that in select. All the connections requests would come to this fd and as soon as you get new client1 connection, create client socket fd and put that into select. Now your select will wait for both, a)new connection request from new client, and b) incoming data from client1.
Have another thread that continuously send data to client. Write a common send function and use mutex to call this function. This way you can make sure that at a time only one thread calls send function.
Regards,
Sumit
- 12-03-2010 #5Just Joined!
- Join Date
- May 2010
- Location
- Madrid
- Posts
- 21
yes, it has more sense.. I have been looking at airserv code, from aircrack-ng suite. It does all that like you say, so I will do the same, seems easier and more optimized
thanks a lot!


Reply With Quote