Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2010
    Location
    Madrid
    Posts
    32

    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:

    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
    	}
    }
    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:
    // 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
    			}
    		}
    	}
    }
    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.

    Should I use mutexs or semaphores to block the socket fd before read and write or it is not necesary?

    Thanks!

  2. #2
    Just 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

  3. #3
    Just Joined!
    Join Date
    May 2010
    Location
    Madrid
    Posts
    32
    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:

    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)
      }
    }
    The pair of threads created for the connected client:

    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
    			}
    		}
    	}
    }
    But I am not sure if this is very.. "academic" : P

    should I use semaphores before read and write the socket fd?

  4. #4
    Just 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

  5. #5
    Just Joined!
    Join Date
    May 2010
    Location
    Madrid
    Posts
    32
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •