c++ - Udp_client_server_using Multi-threading -
i implented basic udp client server application using multi threading thread not invoking...can body me..
class uu_frameworksocket { protected: struct sockaddr_in modulemysocketaddress; uu_int32 modulehostportk; uu_char* modulehostnamek; uu_int32 modulesockflagk; uu_int32* modulesockoptvaluep; public: uu_void uu_moduleinitsocket(uu_int32,uu_int32); uu_void uu_modulesetsocketoptions(uu_int32); uu_void sethostname(uu_char* ); uu_void setportnumber(uu_int32 ); uu_void uu_moduleclosesocket(uu_int32 ); uu_frameworksocket(){ modulehostportk = 3232; modulehostnamek = (uu_char*) malloc(20); strcpy(modulehostnamek,"127.0.0.1"); } }; uu_void uu_frameworksocket :: sethostname(uu_char *pshostname){ modulehostnamek = (uu_char*) malloc(20); strcpy(modulehostnamek,pshostname); } uu_void uu_frameworksocket :: setportnumber(uu_int32 iportnumber){ modulehostportk=iportnumber; } uu_void uu_frameworksocket :: uu_moduleclosesocket(uu_int32 isocketfd){ close(isocketfd); } uu_void uu_frameworksocket :: uu_moduleinitsocket(uu_int32 itypefamily,uu_int32 itypeprotocol){ if(itypefamily == uu_project_module_ipv4) { if(itypeprotocol == uu_project_module_tcp) modulesockflagk = socket(af_inet, sock_stream, 0); else modulesockflagk = socket(af_inet, sock_dgram, 0); } else { if(itypeprotocol == uu_project_module_tcp) modulesockflagk = socket(af_inet6, sock_stream, 0); else modulesockflagk = socket(af_inet6, sock_dgram, 0); } if (modulesockflagk == -1){ printf("error while initializing socket.\n"); exit(0); } } uu_void uu_frameworksocket :: uu_modulesetsocketoptions(uu_int32 itypefamily){ modulesockoptvaluep = (uu_int32*)malloc(sizeof(uu_int32)); *modulesockoptvaluep = 1; if( (setsockopt(modulesockflagk, sol_socket, so_reuseaddr, (uu_char*)modulesockoptvaluep, sizeof(uu_int32)) == -1 ) ){ printf("error while setting socket options.\n"); free(modulesockoptvaluep); exit(0); } free(modulesockoptvaluep); if(itypefamily == uu_project_module_ipv4) modulemysocketaddress.sin_family = af_inet ; else modulemysocketaddress.sin_family = af_inet6 ; modulemysocketaddress.sin_port = htons(modulehostportk); memset(&(modulemysocketaddress.sin_zero), 0, 8); modulemysocketaddress.sin_addr.s_addr = inet_addr(modulehostnamek); } class uu_projectmoduleudp : public uu_frameworksocket { protected: socklen_t modulemysockaddrlength; uu_int32* modulesockconnflag; sockaddr_in moduleipsockaddr; pthread_t modulethreadid; public: struct request{ sockaddr_in moduleclientsockaddr; uu_int32 moduleclientsocket; }; uu_void moduleudpbind(){ if( bind( modulesockflagk, (sockaddr*)&modulemysocketaddress, sizeof(modulemysocketaddress)) == -1 ){ fprintf(stderr,"error while binding socket:%d\n",errno); exit(0); } } uu_void moduleudpaccept(){ modulemysockaddrlength = sizeof(sockaddr_in); uu_char msg[100]; uu_int32 n; struct request modulerequeststructure; while(true){ if(( n= recvfrom( modulesockflagk,msg,100,0, (sockaddr*)&moduleipsockaddr, &modulemysockaddrlength))!= -1){ printf("==\nreceived connection %s\n",inet_ntoa(moduleipsockaddr.sin_addr)); printf("received message: %s \n",msg); sendto( modulesockflagk,msg,n,0, (sockaddr*)&moduleipsockaddr, modulemysockaddrlength); modulerequeststructure.moduleclientsockaddr=moduleipsockaddr; modulerequeststructure.moduleclientsocket=modulesockflagk; pthread_create(&modulethreadid,0,&moduleudpsockethandler, (uu_void*)&modulerequeststructure ); pthread_detach(modulethreadid); } else{ fprintf(stderr, "error accepting... %d\n", errno); } } } static uu_void* moduleudpsockethandler(uu_void* modulerequeststruct){ request *modulerequest=(request *)modulerequeststruct; uu_int32 iclientsocket=modulerequest->moduleclientsocket; sockaddr_in clientsockaddr=modulerequest->moduleclientsockaddr; socklen_t modulemysockaddrlength = sizeof(sockaddr_in); uu_char acbuffer[1024]; uu_int32 ibufferlen = 1024; uu_int32 ibytecount; while(true){ memset(acbuffer, '\0', ibufferlen); if((ibytecount = recvfrom(iclientsocket,acbuffer,ibufferlen,0,(sockaddr*)&clientsockaddr,&modulemysockaddrlength))== -1){ fprintf(stderr, "error receiving data %d\n", errno); exit(0); } printf("received bytes: %d\nreceived string: %s \n", ibytecount, acbuffer); if((ibytecount = sendto(iclientsocket, acbuffer, strlen(acbuffer), 0,(sockaddr*)&clientsockaddr,modulemysockaddrlength))== -1){ fprintf(stderr, "error sending data %d\n", errno); exit(0); } printf("sent bytes %d\n", ibytecount); } } uu_void moduleudpafterconn(){ uu_char acbuffer[1024]; uu_int32 ibytecount; uu_int32 ibufferlen=0; modulemysockaddrlength = sizeof(sockaddr_in); ibufferlen = 1024; while(true){ memset(acbuffer, '\0', ibufferlen); printf("enter text send server (press enter):\n"); fgets(acbuffer, 1024, stdin); acbuffer[strlen(acbuffer)-1]='\0'; if( (ibytecount=sendto(modulesockflagk, acbuffer, strlen(acbuffer),0,(sockaddr*)&modulemysocketaddress,modulemysockaddrlength))== -1){ fprintf(stderr, "error sending data %d\n", errno); exit(0); } printf("sent bytes %d\n", ibytecount); if((ibytecount = recvfrom(modulesockflagk, acbuffer, ibufferlen, 0,null,null))== -1){ fprintf(stderr, "error receiving data %d\n", errno); exit(0); } printf("recieved bytes: %d\nreceived string: \"%s\"\n", ibytecount, acbuffer); } close(modulesockflagk); } }; int main(int argc, char **argv){ if(strcmp(argv[1],"udp")==0){ if(strcmp(argv[2],"server")==0){ printf("this udp server"); uu_projectmoduleudp server; server.setportnumber(6745); server.uu_moduleinitsocket(uu_project_module_ipv4,uu_project_module_udp); server.uu_modulesetsocketoptions(uu_project_module_ipv4); server.moduleudpbind(); server.moduleudpaccept(); } else if(strcmp(argv[2],"client")==0){ printf("this udp client"); uu_projectmoduleudp client; client.setportnumber(6745); client.uu_moduleinitsocket(uu_project_module_ipv4,uu_project_module_udp); client.uu_modulesetsocketoptions(uu_project_module_ipv4); client.moduleudpafterconn(); } } else printf("wrong/missing arguments\n"); return 0; }
here each client thread has invoked not invoking..
what happening thread crashes. reason undefined behavior when try access modulerequest
pointer, points local variable inside moduleudpaccept
.
when moduleudpaccept
returns, pointer no longer pointing valid memory, , undefined behavior, may (or may not) cause thread crash.
Comments
Post a Comment