| aditi88 (14) | |
|
Hello frnds, I have implemented client server arch. wherein I have implemented multithreading on server. This is working perfectly well. But I was unable to check whether really server executes all clients simultaneously since I could not start all clients at exactlt the same time so I tried multithreading on client side as well which is not working properly. ReqClient is a class which has all methods to send req, send data, receive data..etc Plz reply as soon as possible...I am stuck since yest.. 1 or 2 threads dont start at all..msgs are sent properly back to client.. I guess malloc() is causing some problems :( Regards, Aditi Code is as follows: void *createNewClientThread(void *msg) //multithreaded function { ReqClient newClient; int portnum; const char *ip1, *port1, *SessionCode; char *filebuffer; FILE *f; long lsize; size_t result; SessionCode = (char *)msg; newClient.SendRequest("127.0.0.1",2212); //Sending Request to Session Manager newClient.SendData(SessionCode, strlen(SessionCode), 0); //Sending CODE to Session Manager char *buffer = (char *)malloc(255),*addr = (char *)malloc(255); const char *msgg = "hmm"; //Allocate memory for character pointers int bytes = newClient.ReceiveData(buffer, 255, 0); //Receive IP Address and PORT from Session Manager cout<<"Received Data from Session Manager : "<<buffer<<"\n"; strcpy(addr,buffer); //Copy in other pointer so that 'buffer' is not changed ...Remember call by reference newClient.CloseSocket(); //Close Socket of Client ip1 = newClient.getIP(addr); //Get IP Address in ip1 pointer port1 = newClient.getPort(addr); //Get PORT in port1 portnum = atoi(port1); //Convert port1 from ascii to int newClient.newSocket(); //Initialize new socket newClient.SendRequest(ip1, portnum); //Send Request to Response Server bytes = newClient.ReceiveData(buffer, 255, 0); //Receive data from Server cout<<"Received Data from Response Server : "<<buffer<<"\n"; newClient.SendData(msgg, strlen(msgg), 0); //Send msg to Server f = fopen("area.txt","rb+"); //Open file to be sent to Server if(f == NULL) { fputs("File error",stderr); exit(1); } /* Get Size of file */ fseek(f,0,SEEK_END); lsize = ftell(f); rewind(f); filebuffer = (char *)malloc(sizeof(char) * (lsize)); if(filebuffer == NULL) { fputs("Memory error",stderr); exit(2); } result = fread(filebuffer,sizeof(char),lsize,f); //Read file to be sent if(result != lsize) { fputs("Reading error",stderr); exit(3); } fclose(f); //Close file newClient.SendData(filebuffer, lsize, 0); //Send file read into buffer to server newClient.CloseSocket(); //Close Client Socket /* Deallocate pointers used */ free(buffer); free(filebuffer); free(addr); buffer = NULL; filebuffer = NULL; addr = NULL; return NULL; } main(int argc, char *argv[]) { pthread_t thread[argc-1]; int i; char *msg[argc-1]; cout<<"Iam in main\n"; for(i = 0;i < (argc-1);i++) { msg[i] = argv[i+1]; } cout<<"Iam in main after loop\n"; for(i = 0;i < (argc-1);i++) pthread_create(&thread[i], NULL, createNewClientThread, (void *)(msg[i])); cout<<"Threads created\n"; for(i = 0;i < (argc-1);i++) pthread_join(thread[i], NULL); cout<<"I died\n"; exit(0); } | |
|
|
|
| aditi88 (14) | |
|
Plz reply as soon as possible...I am stuck since yest.. 1 or 2 threads dont start at all..msgs are sent properly back to client.. I guess malloc() is causing some problems :( Regards, Aditi | |
|
|
|