TCP Server that handles multiple client requests at the same time by using multiple threads

Dependents:   ThreadServer Server_Multi_Client

Files at this revision

API Documentation at this revision

Comitter:
lemniskata
Date:
Sat Jun 29 23:21:20 2013 +0000
Parent:
1:e1db78d5ccc1
Commit message:
Multi Client server. Handles multiple clients at the same time in different threads

Changed in this revision

Server.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r e1db78d5ccc1 -r c57a998796ae Server.cpp
--- a/Server.cpp	Tue Jun 11 21:11:33 2013 +0000
+++ b/Server.cpp	Sat Jun 29 23:21:20 2013 +0000
@@ -9,15 +9,15 @@
 ** Descriptions:        The original version
 **
 **------------------------------------------------------------------------------------------------------
-** Modified by:            
-** Modified date:    
+** Modified by:            Ivan Shindev
+** Modified date:    06/29/2013
 ** Version:
-** Descriptions:        
+** Descriptions:        Added functionality of running multiple thread instances of the same function
 ********************************************************************************************************/
 #include "Server.h"
 
 #include "mbed.h"
-
+#include "Threads.h"
 /*Handle_client handles the client request
 *
 * Default: Echo the clients request
@@ -40,7 +40,6 @@
 
 }
 
-osThreadDef(Handle_client, osPriorityNormal, DEFAULT_STACK_SIZE);
  
 Server::Server(int port, int max_number_of_clients):
         _port(port), _max_number_of_clients(max_number_of_clients){
@@ -79,20 +78,35 @@
     }
   
     socklen_t newSockRemoteHostLen = sizeof(localHost);
-    int tid;
-  
-    for(tid=0;tid<=_max_number_of_clients;tid++) 
+    
+    ThreadList* my_threads=NULL; //List of all Initialized threads
+    ThreadList* thread; //pointer to the last created ThreadList element
+    int max=_max_number_of_clients;
+    while(1)
     {
         new_socket = lwip_accept(socket_server, (struct sockaddr*) &localHost, &newSockRemoteHostLen);
         if (new_socket < 0)
          {
-            return -1;
+          
+         }
+         else
+         {
+            
+            if(initThread(&my_threads,Handle_client,&thread,max)==0)
+            {
+               lwip_close(new_socket);
+              
+            }
+            else
+            {
+               
+                thread->id=osThreadCreate(thread->thread,(void *) new_socket);
+            
+            }
          }
 
-               osThreadCreate(osThread(Handle_client), (void *) new_socket);
     
    }
-    return 1;
 }