Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1826 connectés 

  FORUM HardWare.fr
  Programmation
  C

  Client & Serveur

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Client & Serveur

n°2281871
sined40
Posté le 22-05-2016 à 00:59:57  profilanswer
 

Bonsoir,
 
Je souhaite écrire un programme permettant à un serveur et plusieurs clients de communiquer via des fifos.
Depuis le programme serveur, je souhaite appeler plusieurs fois un programme via l'appel système execv et un appel à fork afin de créer un process serveur et plusieurs process clients.
 
Après exécution de mon programme et l’exécution dans ma console Ubuntu de la commande "ps", cela ne semble pas marcher comme prévu.
Mes fifos ne sont pas crées correctement coté client :??:  
 
Mon programme n'est qu'une ébauche, je souhaite juste valider que la création des fifos coté client se passe bien.
 
Code MyClient.c

Code :
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6. #define CLIENT_PIPE     "NamedPipeClient"
  7. int main(int argc,char* argv[])
  8. {
  9.     char buf[128];
  10.    
  11.     sprintf(buf,"%s%d", CLIENT_PIPE, atoi(argv[1]));
  12.    
  13.     /* Creation of the fifo */
  14.     umask(0);
  15.     if((mknod(buf, S_IFIFO | 0666, 0)) !=0)
  16.     {
  17.         printf("Error on the client fifo creation (%s).\n", CLIENT_PIPE);
  18.         return EXIT_FAILURE;
  19.     }
  20.    
  21.     printf("Client %d\n", atoi(argv[1]));
  22.    
  23.     /* Infinite loop */
  24.     while(1)
  25.     {
  26.         ;
  27.     }
  28.    
  29.     return EXIT_SUCCESS;
  30. }


 
Code MyServer.c

Code :
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6. #define NBR_MAX_CLIENT  5
  7. #define SERVER_PIPE     "NamedPipeServ"
  8. int main(int argc,char* argv[])
  9. {
  10.     int ClientId;
  11.     pid_t PoolCltPid[NBR_MAX_CLIENT];
  12.     pid_t ParentId;
  13.     char* ExecArgs[]={"./MyClient.exe", "MyClient.exe", 0, "NULL"};
  14.    
  15.     /* Init */
  16.     ClientId = 0;
  17.     memset((void*) PoolCltPid, '\0', sizeof(PoolCltPid));
  18.    
  19.     /* Creation of the fifo */
  20.     umask(0);
  21.     if((mknod(SERVER_PIPE, S_IFIFO | 0666, 0)) !=0)
  22.     {
  23.         printf("Error on the server fifo creation (%s).\n", SERVER_PIPE);
  24.         return EXIT_FAILURE;
  25.     }
  26.    
  27.     /* Get the parent PID */
  28.     ParentId = getpid();
  29.    
  30.     /* Create nbr max of client */
  31.     for(ClientId = 0; ClientId < NBR_MAX_CLIENT; ClientId++)
  32.     {
  33.         /* Create a child process */
  34.         if (getpid() == ParentId)
  35.         {
  36.             PoolCltPid[ClientId] = fork();
  37.            
  38.             /* Child process pid equal 0 */
  39.             if (PoolCltPid[ClientId] == 0)
  40.             {
  41.                 printf(" |_Client Id: %d\n", ClientId);
  42.                 sprintf(ExecArgs[2], "%d", ClientId);
  43.                 execv((char*) ExecArgs[0],(char* const*) ExecArgs);
  44.             }
  45.         }
  46.     }
  47.    
  48.     /* Infinite loop */
  49.     while(1)
  50.     {
  51.         ;
  52.     }
  53.    
  54.     return EXIT_SUCCESS;
  55. }

mood
Publicité
Posté le 22-05-2016 à 00:59:57  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  Client & Serveur

 

Sujets relatifs
Client/Serveur PythonClient / Serveur => interblocage
Besoin d'aide, client serveur openSSL - polarSSL pour communication HTTPS client <- > serveur
serveur et client réseau sur même machineMettre classe client et classe serveur dans un seul main
DLNA Client/Serveurapplication client serveur
problème client-serveur (socket) 
Plus de sujets relatifs à : Client & Serveur


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR