Changar Posté(e) le 3 juin 2008 Partager Posté(e) le 3 juin 2008 (modifié) Bonjour, Voilà petit problème voir gros sur mon serveur multiclient. Mon probleme,quand mon serveur à accepté 2 clients voir plus,il ne traite la reception de donnée qu'une fois sur 2.J'ai mis des api_sleep sur mes clients pour voir si ça ne venait pas de là mais non ça à l'air de venir du serveur. Désolé ça va faire un peu lourd #include<winsock.h> // serveur3.cpp #include<iostream.h> // serveur echo TCP/IP multi client #include<conio.h> #include<stdio.h> #include<string.h> #include <io.h> #include<string.h> #define __WIN__ #include "mysql.h" typedef struct { SOCKET server, client; UINT numero; } sock_num; int InitWinsock(void) { WORD Version = MAKEWORD(1,1); WSADATA WSAData; /* structure WSADATA définie dans winsock.h */ int err = WSAStartup(Version, &WSAData); /* appel de notre fonction */ if(err == 0) fprintf(stdout, " [*] DLL WinSock chargee avec succes.\n"); else { fprintf(stderr, "Erreur lors de l'initialisation de Winsock, code d'erreur : %d\n", GetLastError()); return(-1); } return(0); } SOCKET CreateSocket(void) { SOCKET sock; /* déclaration de notre socket */ struct sockaddr_in sin; /* déclaration de la structure sockaddr_in */ int port; memset(&sin, 0x0, sizeof(struct sockaddr_in)); cout << "entrer le numero de port" << endl; cin >> port; sin.sin_addr.s_addr = INADDR_ANY; /* définis l'adresse du server */ sin.sin_family = AF_INET; /* famille du socket */ sin.sin_port = htons(port); /* port sur lequel va etre assigner le server */ sock = socket(AF_INET, SOCK_STREAM, 0); /* appel de la fonction socket */ if(sock != INVALID_SOCKET) fprintf(stdout, " [*] Socket cree avec succes.\n"); else { fprintf(stderr, "Erreur lors de la creation du socket, code d'erreur : %d\n", WSAGetLastError()); return(-1); } if(bind(sock, (sockaddr *)&sin, sizeof(struct sockaddr_in)) != SOCKET_ERROR) /* associe l'adresse local au socket */ fprintf(stdout, " [*] Adresse local associee au socket avec succes.\n"); else { fprintf(stderr, "Erreur lors de l'association de l'adresse local au socket, code d'erreur : %d\n", WSAGetLastError()); return(-1); } return(sock); /* retourne le socket */ } void info_serveur() { char nom[256], host[36]; char *adrIPp; struct hostent *hp; struct in_addr ip_serveur; gethostname(nom,sizeof(nom)); cout << "nom du serveur : " << nom << endl; if((hp=gethostbyname(nom))==NULL) { fprintf(stderr, "machine hebergeant le programme non identifiee par gethostbyname\n"); exit(1); } ip_serveur.s_addr=*(int *)hp->h_addr; adrIPp =(char *)inet_ntoa(ip_serveur); printf("IP du serveur : %s\n", adrIPp); } void info_client(SOCKET sock_client) { char *ip_client; int longueur; struct sockaddr_in adr_client; struct hostent *h_client; longueur=sizeof(adr_client); getpeername(sock_client, (struct sockaddr *) &adr_client, &longueur); h_client = gethostbyaddr ((char *)&adr_client.sin_addr,longueur,AF_INET); //cout << "nom du client : " << h_client->h_name << endl; ip_client =(char *)inet_ntoa(adr_client.sin_addr); printf("IP du client : %s\n", ip_client); } //DWORD WINAPI traite_connexion(sock_num SDonnee) DWORD WINAPI traite_connexion(LPVOID Donnee) { int tps; char tab[50]; int ret; string vartransfert; MYSQL *mySQL; MYSQL_ROW myROW; MYSQL_RES *myRES; char *buffer = NULL; buffer = (char *)malloc(1024 * sizeof(char)); sock_num *SDonnee = (sock_num*) Donnee; if(buffer == NULL) { printf("Erreur d'allocation memoire\n"); exit(-1); } info_client(SDonnee->client); for(; { ret = recv(SDonnee->client, buffer, 1024, 0); // reception et stockage des données dans buffer buffer[ret] = '\0'; // ajout du caractère de fin pour le strlen() qui va suivre if(ret != SOCKET_ERROR) cout << "recu: "<< buffer << endl; else { printf("Erreur reception donnees : %d\n", WSAGetLastError()); break; } ret = send(SDonnee->client, buffer, strlen(buffer), 0); // envois du contenu de buffer au client mySQL = mysql_init(NULL); vartransfert=buffer; //Connection a la bdd if (!mysql_real_connect(mySQL, "127.0.0.1", "root", "", "Festo", 0, NULL, 0)) {printf("connection a la bdd echouee");// la connection a échoué } else { if(vartransfert=="ejection\0")mysql_query(mySQL, "UPDATE testeur SET PistonEjecte=PistonEjecte+1") ; if(vartransfert=="coussin\0")mysql_query(mySQL, "UPDATE testeur SET CoussinAir=CoussinAir+1") ; if(vartransfert=="ascenseur\0")mysql_query(mySQL, "UPDATE testeur SET Ascenseur=Ascenseur+1") ; if(vartransfert=="piecetestee\0")mysql_query(mySQL, "UPDATE testeur SET PiecesTestees=PiecesTestees+1") ; if(vartransfert=="nonvalide\0")mysql_query(mySQL, "UPDATE testeur SET PieceNonValide=PieceNonValide+1"); if(vartransfert=="Eteint\0")mysql_query(mySQL, "UPDATE convoyeur SET Etat='Eteint'"); if(vartransfert=="Allumer\0")mysql_query(mySQL, "UPDATE convoyeur SET Etat='Allumer'"); if(vartransfert=="stop1\0")mysql_query(mySQL, "UPDATE convoyeur SET Nbr_Stop_Action_api1 =Nbr_Stop_Action_api1+1 "); if(vartransfert=="char0.1\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api1=0"); if(vartransfert=="char1.1\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api1=1"); if(vartransfert=="char2.1\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api1=2"); if(vartransfert=="char3.1\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api1=3"); if(vartransfert=="char4.1\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api1=4"); if(vartransfert=="char5.1\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api1=5"); if(vartransfert=="char6.1\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api1=6"); if(vartransfert=="stop2\0")mysql_query(mySQL, "UPDATE convoyeur SET Nbr_Stop_Action_api2 =Nbr_Stop_Action_api2+1 "); if(vartransfert=="char0.2\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api2=0"); if(vartransfert=="char1.2\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api2=1"); if(vartransfert=="char2.2\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api2=2"); if(vartransfert=="char3.2\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api2=3"); if(vartransfert=="char4.2\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api2=4"); if(vartransfert=="char5.2\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api2=5"); if(vartransfert=="char6.2\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api2=6"); if(vartransfert=="stop3\0")mysql_query(mySQL, "UPDATE convoyeur SET Nbr_Stop_Action_api3 =Nbr_Stop_Action_api3+1 "); if(vartransfert=="char0.3\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api3=0"); if(vartransfert=="char1.3\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api3=1"); if(vartransfert=="char2.3\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api3=2"); if(vartransfert=="char3.3\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api3=3"); if(vartransfert=="char4.3\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api3=4"); if(vartransfert=="char5.3\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api3=5"); if(vartransfert=="char6.3\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api3=6"); if(vartransfert=="stop4\0")mysql_query(mySQL, "UPDATE convoyeur SET Nbr_Stop_Action_api4 =Nbr_Stop_Action_api4+1 "); if(vartransfert=="char0.4\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api4=0"); if(vartransfert=="char1.4\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api4=1"); if(vartransfert=="char2.4\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api4=2"); if(vartransfert=="char3.4\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api4=3"); if(vartransfert=="char4.4\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api4=4"); if(vartransfert=="char5.4\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api4=5"); if(vartransfert=="char6.4\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api4=6"); if(vartransfert=="stop5\0")mysql_query(mySQL, "UPDATE convoyeur SET Nbr_Stop_Action_api5 =Nbr_Stop_Action_api5+1 "); if(vartransfert=="char0.5\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api5=0"); if(vartransfert=="char1.5\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api5=1"); if(vartransfert=="char2.5\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api5=2"); if(vartransfert=="char3.5\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api5=3"); if(vartransfert=="char4.5\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api5=4"); if(vartransfert=="char5.5\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api5=5"); if(vartransfert=="char6.5\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api5=6"); if(vartransfert=="stop6\0")mysql_query(mySQL, "UPDATE convoyeur SET Nbr_Stop_Action_api6 =Nbr_Stop_Action_api6+1 "); if(vartransfert=="char0.6\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api6=0"); if(vartransfert=="char1.6\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api6=1"); if(vartransfert=="char2.6\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api6=2"); if(vartransfert=="char3.6\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api6=3"); if(vartransfert=="char4.6\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api6=4"); if(vartransfert=="char5.6\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api6=5"); if(vartransfert=="char6.6\0")mysql_query(mySQL, "UPDATE convoyeur SET Num_chariot_api6=6"); else{tps=atoi(buffer);sprintf(tab,"UPDATE convoyeur SET Tps_de_fct=Tps_de_fct+%d",tps); mysql_query(mySQL,tab);} }mysql_close(mySQL); if(ret != SOCKET_ERROR) fprintf(stdout, " [*] Donnees envoyer.\n"); else { printf("Erreur envoi des donnees : %d\n", WSAGetLastError()); break; } } closesocket(SDonnee->client); // ferme le socket gerant la connexion cliente free(buffer); // libère la mémoire allouée pour notre buffer } int Server(SOCKET server_socket) { SOCKET client_socket; struct sockaddr_in adresse; int adresse_size = 0; memset(&adresse, 0, sizeof(struct sockaddr_in)); HANDLE hThread; DWORD ThreadID; sock_num *SData = new sock_num; SData->numero = 0; SData->server = server_socket; if(listen(server_socket, 3) == 0) fprintf(stdout, "passage en mode ecoute\n"); else { printf("Erreur listen : %d\n", WSAGetLastError()); return(-1); } do{ adresse_size = sizeof(adresse); SData->client = accept(server_socket, (struct sockaddr *)&adresse, &adresse_size); if(SData->client == INVALID_SOCKET) { printf("erreur acceptation du client = %d\n", WSAGetLastError()); return(-1); } else { fprintf(stdout, " [*] Client accepte avec succes\n"); SData->numero++; printf("client %d [%s]\n",SData->numero,inet_ntoa(adresse.sin_addr)); hThread = CreateThread(NULL,0,traite_connexion,(LPVOID)SData,0,&ThreadID); // passage de paramètres à MonThread par le pointeur Data if (!hThread) { puts("erreur createprocess\n"); close(SData->client); } } } while(1); } void main(void) { SOCKET socket_server; InitWinsock(); socket_server = CreateSocket(); info_serveur(); Server(socket_server); WSACleanup(); } Modifié le 3 juin 2008 par Changar Lien vers le commentaire Partager sur d’autres sites More sharing options...
ol_4ever69 Posté(e) le 3 juin 2008 Partager Posté(e) le 3 juin 2008 mets ces balises : texte [ /spoiler] (sans espaces dans les balises) pour réduire ton texte. Lien vers le commentaire Partager sur d’autres sites More sharing options...
Messages recommandés
Veuillez vous connecter pour commenter
Vous pourrez laisser un commentaire après vous êtes connecté.
Connectez-vous maintenant