Problème do while

Fermé
emmy_2019 Messages postés 59 Date d'inscription mardi 23 avril 2019 Statut Membre Dernière intervention 16 septembre 2019 - Modifié le 2 mai 2019 à 11:10
[Dal] Messages postés 6174 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 2 février 2024 - 6 mai 2019 à 17:59
Bonjour,

J'ai problème que si la condition de "do While" est présente le programme ne sort pas de cette boucle et il ne m'affiche pas le message après ce dernière. Voilà mon code :
Comptage *  iris_http_tcp_Counting_Passengers()
{// Initialize Winsock.

 //int i;
 char *DoorState1;
 char *DoorState2;
 char *DoorState3;
 char Value1[] ="SingleDoorClose";
 char Value2[] ="SingleDoorClose";
 char Value3[] ="SingleDoorClose";
 char recvbuf[1024] = "";
    int recvbuflen = DEFAULT_BUFLEN;
    struct sockaddr_in service;
    WSADATA wsaData;
    SOCKET ListenSocket;
    SOCKET AcceptSocket;
 char *reply = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 12\n\nAll received";
  SYSTEMTIME st;
  char buffer[80];
  int count = 0;
  Comptage Count_Pass[512];
  char TagValue[100]= {0};
  char TagValue1[100]= {0};
  char TagValue2[100]= {0};
  char TagValue3[100]= {0};
  char Tag1[] = "Value";

  int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);

  if (iResult != NO_ERROR)

    LOG_INFO("Server: Error at WSAStartup().\n");

  else

    LOG_INFO("Server: WSAStartup() is OK.\n");






  // The sockaddr_in structure specifies the address family,

  // IP address, and port for the socket that is being bound.


  service.sin_family = AF_INET;

  service.sin_addr.s_addr = inet_addr("192.168.0.95");

  service.sin_port = htons(8080);


  
  // Create a SOCKET for listening for

  // incoming connection requests.

  ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (ListenSocket == INVALID_SOCKET)

  {

    LOG_INFO("Server: Error at socket(): %ld\n", WSAGetLastError());

    WSACleanup();

    return 0;

  }

  else

    LOG_INFO("Server: socket() is OK.\n");


  if (bind(ListenSocket, (SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR)

  {

    LOG_INFO("Server: bind() failed.\n");

    WSACleanup();

    return -1;

  }

  else

    LOG_INFO("Server: bind() is OK.\n");



   // Listen for incoming connection requests on the created socket
  if (listen(ListenSocket, 10) == SOCKET_ERROR)
  {
    LOG_INFO("Server: Error listening on socket.\n");
 WSACleanup();
 return -1;
  }

  else

    LOG_INFO("Server: listen() is OK.\n");



  // Create a SOCKET for accepting incoming requests.

  // Accept the connection if any...



 DoorState1 = iris_http_tcp_doorState1();
 DoorState2 = iris_http_tcp_doorState2();
 DoorState3 = iris_http_tcp_doorState3();
 LOG_INFO("DoorState1  =%s  DoorState2  =%s  DoorState3  =%s\n\n", DoorState1, DoorState2, DoorState3);
 do
 {
      AcceptSocket = accept(ListenSocket, (struct sockaddr*)&IRMAClient, &iIRMAClientLen);

            if (AcceptSocket == INVALID_SOCKET)

           {

                LOG_INFO("Server: accept() error %d\n", WSAGetLastError());

                WSACleanup();

                return -1;

            }

           else

              LOG_INFO("Server: accept() is OK.\n");

              LOG_INFO("Server: accepted connection from %s, port %d\n", inet_ntoa(IRMAClient.sin_addr), htons(IRMAClient.sin_port)) ;


        iResult = recv(AcceptSocket, recvbuf, 1024, 0);
  //time (&rawtime);
   //timeinfo = localtime (&rawtime);

  //strftime (buffer,80,"%Y%m%d-%X",timeinfo);

        if (iResult == SOCKET_ERROR)

              {

            LOG_INFO("Server: recv() failed: error %d\n", WSAGetLastError());

            closesocket(AcceptSocket);

            continue;

        }

       else

            LOG_INFO("Server: recv() is OK.\n");

  if (iResult == 0)

        {

            printf("Server: Client closed connection.\n");

            closesocket(AcceptSocket);

            continue;

        }
  GetLocalTime(&st);
  sprintf(Count_Pass[count].Heure, "%04d%02d%02d-%02d:%02d:%02d" ,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
  GetXmlTagValue(recvbuf,"DoorID",Tag1,TagValue);
  GetXmlTagValue(recvbuf,"In",Tag1,TagValue1);
  GetXmlTagValue(recvbuf,"Out",Tag1,TagValue2);
  Count_Pass[count].ID = atoi(TagValue);
  Count_Pass[count].Nombre_Mont = atoi(TagValue1);
  Count_Pass[count].Nombre_Desc = atoi(TagValue2);
        LOG_INFO("Server: recvbuf = %s\n  Time = %s , ID= %d  Nombre de Montée=%d, Nombre de Desc=%d", recvbuf, Count_Pass[count].Heure, Count_Pass[count].ID, Count_Pass[count].Nombre_Mont, Count_Pass[count].Nombre_Desc);


 send(AcceptSocket, reply, strlen(reply), 0);
 LOG_INFO("%s\n",reply);
 closesocket(AcceptSocket);
 count++;
 continue;
}while (DoorState1 != Value1 && DoorState2 != Value2 && DoorState3 != Value3);
 LOG_INFO("DoorState1  =%s  DoorState2  =%s  DoorState3  =%s\n\n", DoorState1, DoorState2, DoorState3);
  return Count_Pass;
}


Avez-vous une idée pourquoi le message DoorState = etc ne s'apparait pas ? Malgré que quand les portes sont fermées dès le début il ne rentre pas dans le do while et il affiche le message mais au moment que les portes sont ouvertes je fais mes traitements et quand je ferme les portes il ne sort pas de la boucle.
Merci par avance de votre aide

Configuration: Windows / Chrome 73.0.3683.103

1 réponse

jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 4 649
2 mai 2019 à 12:14
Bonjour,

Je ne programme pas en C ... mais....
A quel endroit, dans ta boucle, mets tu à jour les variables Valeur1, Valeur2, Valeur3 ?
A moins que ça ne soit les DoorState1 DoorState2 DoorState3 que tu ne remets pas à jour à l'intérieur de ta boucle...
Dans tous les cas, ces variables n'étant pas modifiées (DANS la boucle), elles ne validerons jamais ta condition WHILE

0
[Dal] Messages postés 6174 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 2 février 2024 1 083
Modifié le 2 mai 2019 à 13:34
en outre, s'il s'agit de comparer le contenu de chaînes de caractères C, il faut utiliser la fonction
strcmp()
et non pas l'opérateur
!=
...
0
emmy_2019 Messages postés 59 Date d'inscription mardi 23 avril 2019 Statut Membre Dernière intervention 16 septembre 2019 > [Dal] Messages postés 6174 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 2 février 2024
2 mai 2019 à 14:11
Merci jordane45 et Dal

en fait même avec strcmp rien n'est changé :

Comptage *  iris_http_tcp_Counting_Passengers()
{// Initialize Winsock.

	//int i;
	char *DoorState1;
	char *DoorState2;
	char *DoorState3;
	char Value1[] ="SingleDoorOpen";
	char Value2[] ="SingleDoorOpen";
	char Value3[] ="SingleDoorOpen";
	char recvbuf[1024] = "";
    int recvbuflen = DEFAULT_BUFLEN;
    struct sockaddr_in service;
    WSADATA wsaData;
    SOCKET ListenSocket;
    SOCKET AcceptSocket;
	char *reply = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 12\n\nAll received";
	 SYSTEMTIME st;
	 char buffer[80];
	 int count = 0;
	 Comptage Count_Pass[512];
	 char TagValue[100]= {0};
	 char TagValue1[100]= {0};
	 char TagValue2[100]= {0};
	 char TagValue3[100]= {0};
	 char Tag1[] = "Value";

  int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);

  if (iResult != NO_ERROR)

    LOG_INFO("Server: Error at WSAStartup().\n");

  else

    LOG_INFO("Server: WSAStartup() is OK.\n");






  // The sockaddr_in structure specifies the address family,

  // IP address, and port for the socket that is being bound.


  service.sin_family = AF_INET;

  service.sin_addr.s_addr = inet_addr("192.168.0.95");

  service.sin_port = htons(8080);


  
  // Create a SOCKET for listening for

  // incoming connection requests.

  ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (ListenSocket == INVALID_SOCKET)

  {

    LOG_INFO("Server: Error at socket(): %ld\n", WSAGetLastError());

    WSACleanup();

    return 0;

  }

  else

    LOG_INFO("Server: socket() is OK.\n");


  if (bind(ListenSocket, (SOCKADDR*) &service,	sizeof(service)) == SOCKET_ERROR)

  {

    LOG_INFO("Server: bind() failed.\n");

    WSACleanup();

    return -1;

  }

  else

    LOG_INFO("Server: bind() is OK.\n");



   // Listen for incoming connection requests on the created socket
  if (listen(ListenSocket, 10) == SOCKET_ERROR)
  {
    LOG_INFO("Server: Error listening on socket.\n");
	WSACleanup();
	return -1;
  }

  else

    LOG_INFO("Server: listen() is OK.\n");



  // Create a SOCKET for accepting incoming requests.

  // Accept the connection if any...

	do
	{
		
			DoorState1 = iris_http_tcp_doorState1();
			DoorState2 = iris_http_tcp_doorState2();
			DoorState3 = iris_http_tcp_doorState3();

			LOG_INFO("DoorState1  =%s  DoorState2  =%s  DoorState3  =%s\n\n", DoorState1, DoorState2, DoorState3);
			AcceptSocket = accept(ListenSocket, (struct sockaddr*)&IRMAClient, &iIRMAClientLen);

				if (AcceptSocket == INVALID_SOCKET)

			   {

					LOG_INFO("Server: accept() error %d\n", WSAGetLastError());

					WSACleanup();

					return -1;

				}

			   else

				  LOG_INFO("Server: accept() is OK.\n");

				  LOG_INFO("Server: accepted connection from %s, port %d\n", inet_ntoa(IRMAClient.sin_addr), htons(IRMAClient.sin_port)) ;


			iResult = recv(AcceptSocket, recvbuf, 1024, 0);
			//time (&rawtime);
			 //timeinfo = localtime (&rawtime);

			//strftime (buffer,80,"%Y%m%d-%X",timeinfo);

			if (iResult == SOCKET_ERROR)

				  {

				LOG_INFO("Server: recv() failed: error %d\n", WSAGetLastError());

				closesocket(AcceptSocket);


			}

		   else

				LOG_INFO("Server: recv() is OK.\n");

			if (iResult == 0)

			{

				printf("Server: Client closed connection.\n");

				closesocket(AcceptSocket);


			}
			GetLocalTime(&st);
			sprintf(Count_Pass[count].Heure, "%04d%02d%02d-%02d:%02d:%02d" ,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
			GetXmlTagValue(recvbuf,"DoorID",Tag1,TagValue);
			GetXmlTagValue(recvbuf,"In",Tag1,TagValue1);
			GetXmlTagValue(recvbuf,"Out",Tag1,TagValue2);
			Count_Pass[count].ID = atoi(TagValue);
			Count_Pass[count].Nombre_Mont = atoi(TagValue1);
			Count_Pass[count].Nombre_Desc = atoi(TagValue2);
			LOG_INFO("Server: recvbuf = %s\n  Time = %s , ID= %d  Nombre de Montée=%d, Nombre de Desc=%d", recvbuf, Count_Pass[count].Heure, Count_Pass[count].ID, Count_Pass[count].Nombre_Mont, Count_Pass[count].Nombre_Desc);


		send(AcceptSocket, reply, strlen(reply), 0);
		LOG_INFO("%s\n",reply);
		closesocket(AcceptSocket);
		count++;
		//LOG_INFO("DoorState1  =%s  DoorState2  =%s  DoorState3  =%s\n\n", iris_http_tcp_doorState1(), iris_http_tcp_doorState2(), iris_http_tcp_doorState3());
		

}while (strcmp(DoorState1,Value1) !=0 && strcmp(DoorState2,Value2) !=0 && strcmp(DoorState3,Value3) !=0);

		LOG_INFO("The doors are closed");
  return Count_Pass;
}


au début comme j'ai cité quand les portes ouvertes j'aurais les données et d'un coup je ferme les porte le message après le boucle while ne s'affiche jamais et du coup le return ne passe pas.
0
[Dal] Messages postés 6174 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 2 février 2024 1 083
Modifié le 2 mai 2019 à 14:24
tu veux boucler tant que les chaînes DoorState1, DoorState2 ou DoorState3 contiennent toutes autre chose que "SingleDoorOpen" ? ... c'est a priori ce que testent tes conditions...

http://www.cplusplus.com/reference/cstring/strcmp/
0
emmy_2019 Messages postés 59 Date d'inscription mardi 23 avril 2019 Statut Membre Dernière intervention 16 septembre 2019 > [Dal] Messages postés 6174 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 2 février 2024
Modifié le 2 mai 2019 à 14:38
Oui DoorState{x} = SingleDoorOpen or SingleDoorClosed
je veux boucler jusqu'à que les chaines vont égale à autre chose que SingleDoorOpen (donc SingleDoorClose)

Remarque : le DoorState est le retour d'un socket tcp (recv())

Mon soucis que j'aurais jamais le message après le boucle
0
emmy_2019 Messages postés 59 Date d'inscription mardi 23 avril 2019 Statut Membre Dernière intervention 16 septembre 2019 > [Dal] Messages postés 6174 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 2 février 2024
2 mai 2019 à 14:38
sur mon console j'ai eu cela :

2019-05-02 14:38:48,098 INFO - --start----
2019-05-02 14:38:48,100 INFO - Server: WSAStartup() is OK.

2019-05-02 14:38:48,102 INFO - Server: socket() is OK.

2019-05-02 14:38:48,104 INFO - Server: bind() is OK.

2019-05-02 14:38:48,106 INFO - Server: listen() is OK.

2019-05-02 14:38:48,108 INFO - La socket 15 est ouverte en TCP/IP.

2019-05-02 14:38:48,109 INFO - Connexion a 192.168.0.130 sur le port 5211

2019-05-02 14:38:48,132 INFO - La socket 17 est ouverte en TCP/IP.

2019-05-02 14:38:48,134 INFO - Connexion a 192.168.0.131 sur le port 5211

2019-05-02 14:38:48,186 INFO - La socket 19 est ouverte en TCP/IP.

2019-05-02 14:38:48,187 INFO - Connexion a 192.168.0.132 sur le port 5211

2019-05-02 14:38:48,210 INFO - DoorState1 =SingleDoorClosed DoorState2 =SingleDoorClosed DoorState3 =SingleDoorClosed
et le message "The doors are closed" ne s'affiche pas
0