From f641e76a05478029cf38b3009d98f6b2c442a435 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 14 Feb 2017 09:33:48 +0100 Subject: [PATCH] Thread implementation ALMOST finished --- .server.c.swp | Bin 0 -> 20480 bytes .server.h.swp | Bin 0 -> 12288 bytes lib.c | 2 +- lib.h | 3 ++ lib.o | Bin 11760 -> 11760 bytes makefile | 2 +- server.c | 145 +++++++++++++++++++++++++++++++++++++------------- server.h | 16 ++++++ server.o | Bin 6968 -> 8792 bytes 9 files changed, 130 insertions(+), 38 deletions(-) create mode 100644 .server.c.swp create mode 100644 .server.h.swp diff --git a/.server.c.swp b/.server.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..84be80d476f94e5db77656994ddb107a4144dd08 GIT binary patch literal 20480 zcmeI2dyHIF9mj70OQ*DjR{W=$cv#%+%y#xM+bykqkkEINK4_Mu)$L}uJ9B3Dc4zM$ z@4d5+HZ2;B6GucmO=iYPA-ZjPgj=uoHHwJDrZ#6-)(XC+G-kU4it+ zrQcclk2#u~MggP16)8{;D;=vBS}S@A-75X5+0BnV2Fds7Tn|YKElduO?!%FCd0(8Ry z`1OsJbpj5;Ubq)}LBJ>BR#*ay;TE_V7QsST05`#W$iO@}Ki9Hegg)dq6b&G1D0{vD0?#VJUPb0^O{PRzR#5lfJae*|Htlfma$$X|SPxOW*d-Zr1f{ z7gR$`*iP7`k_(kwWpnkm>lqY#$yQW6U+1cA7leMPHkHL30d~!cmgrcJD9egnuZ9gn z(`uo$4@EelDpqvdvYezWk@n?G2F04F}h3{ zwYUH>E>8G%4STdVOChi9d!@*UEXCGBzZm93DnXzMLR2iXMTVu3R8HDBr4ie2_P&9h z?!mYL+G=YBl?;8`4JwjkUfm6?jC!O!>A|W#`a({O+1097%8J;WqFn^eLFrZW{hUzd zZ9J;fet(Z*sd_0CN+#_ajw@0=61S4Bv)y$<$F5ScPK&_Tt_u|go7vmSCoF5+bIR>B zU3L16+KIE#rL+xjkLCw=^JQ4~UD2)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kPO@MEt?CPIgM;s z$OKbpbR4IO_A@=u=sy$`z2upXHM#}E5LAaY!>o6{SOzDG79=qC;$4t+pDwS{0rZF-5msE|*&bB|{%z4bcgl=BU_ zs3S{%^j33L5i|v!n^kydcG4))rcm8WrlY#g)b(dvn>H;&S`OvmT0+iq!|(Fqwx!)B rk+p>HPZ?fW-AXN3f|kAN`;`@RhhVIQo*^0Q@T~`sdr= 3 ){ - - if( strlen(argv[2]) > BUFSIZE || sscanf(argv[2], "%s", to_send) <= 0 ){ - printf("argument error: message must be a string (max: %d characters long)\n", BUFSIZE); - return 1; - } - - }else - strcpy(to_send, "server default message"); - - /* [3] Create socket @@ -63,20 +49,103 @@ int main(int argc, char* argv[]){ } - /* [4] Wait for client + /* [4] Wait for clients indefinately =========================================================*/ - /* (1) Listen to incoming connection */ - datasock = xlisten(listensock, &addr_client); + int i, index = -1; // thread index + while( 1 ){ + + /* (1) Listen for incoming connection */ + index = -1; + DEBUG&& debug("server", "wait for client"); + datasock = xlisten(listensock, &addr_client); + + /* (2) Manage error */ + if( datasock == -1 ){ + DEBUG&& printf("error\n"); + continue; + } + + DEBUG&& printf("done\n"); + + + /* (3) Search for a free manager */ + DEBUG&& debug("server", "finding free thread"); + for( i = 0 ; i < maxClients ; i++ ) + + if( a_managers[i] == 0 ){ + index = i; + break; + } + + + /* (4) If we haven't found a free manager -> do nothing (john snow) */ + if( index == -1 ){ + DEBUG&& printf("not found\n"); + continue; + } + + DEBUG&& printf("found\n"); + + /* (5) Build thread arguments */ + built_args.socket = datasock; + built_args.index = index; + + /* (6) Processing client connection on a new thread */ + pthread_create(&managers[index], NULL, manageClient, (void*)(intptr_t) &built_args); + + /* (7) Mark the manager as active */ + a_managers[index] = 1; + - /* (2) Manage error */ - if( datasock == -1 ){ - perror("erreur connection client"); - exit(1); } + /* [5] Wait for all threads to end + =========================================================*/ + /* (1) Wait for all threads to end properly */ + for( i = 0 ; i < maxClients ; i++ ) + pthread_join(managers[i], NULL); + + /* (2) Close listening socket */ + close(listensock); + + return 0; +} - /* [5] Wait for data + + + + + + + + +void* manageClient(void* pthread_args){ + + + /* [1] Initialization + =========================================================*/ + /* (1) Initialize socket data */ + struct sockaddr_in addr_client; + char* to_send = (char*) malloc( BUFSIZE * sizeof(char) ); + char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) ); + int bytes; // transfer count + char client_ip[20]; + + /* (2) Fetch arguments */ + pthread_arg_wrapper* arguments = (pthread_arg_wrapper*) (intptr_t) pthread_args; + + // get client_socket + int datasock = arguments->socket; + int thread_index = arguments->index; + + strcpy(to_send, "server default response"); + + + DEBUG&& debug("thread", "starting thread"); + DEBUG&& printf("success[%d]\n", thread_index); + + /* [2] Wait for data =========================================================*/ /* (1) wait/read data */ bytes = xread(datasock, to_recv, BUFSIZE); @@ -92,9 +161,10 @@ int main(int argc, char* argv[]){ inet_ntop(AF_INET, &(addr_client.sin_addr), client_ip, 20); printf("*** received '%s' (%d bytes) from %s:%d\n", to_recv, bytes, client_ip, ntohs(addr_client.sin_port)); - - /* [6] Send response + + + /* [3] Send response =========================================================*/ /* (1) Send response */ bytes = xwrite(datasock, to_send); @@ -106,10 +176,13 @@ int main(int argc, char* argv[]){ } - /* [6] Close socket - =========================================================*/ - close(datasock); - close(listensock); - return 0; + /* [4] End process + =========================================================*/ + /* (1) Close data socket */ + close(datasock); + + /* (2) Mark manager as inactive again */ + managers[thread_index] = 0; + } diff --git a/server.h b/server.h index 085b371..1dcad0f 100644 --- a/server.h +++ b/server.h @@ -14,9 +14,25 @@ #include #include #include + #include #include "lib.h" + // DEFINITIONS + typedef struct{ + int socket; + int index; + } pthread_arg_wrapper; + + + // VARIABLES + static pthread_t managers[maxClients]; // containa THREADS + static int a_managers[maxClients] = {0}; // contains THREADS actifs + + /************************************************ + **** Signatures **** + ************************************************/ + void* manageClient(void* pthread_args); #endif diff --git a/server.o b/server.o index 868c4a5b2661239c80ee2a7ed2b193172cb8daad..b91c6576f48562e00bd65d7b7ac15d1e499daf03 100644 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