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

  FORUM HardWare.fr
  Programmation
  PHP

  [RESOLU] socket inter-process C/PHP linux

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[RESOLU] socket inter-process C/PHP linux

n°2158788
ti7bo7
Posté le 02-10-2012 à 00:26:20  profilanswer
 

Bonjour,
 
J'aimerai communiquer entre deux processus sous linux. J'ai un programme en C (serveur) et un autre en PHP (client).  
 
Le programme en C démarre bien et j'ai bien mon fichier server dans le dossier tmp.
 
Par contre, le fichier php je suis un peu perdu avec le client. Je ne sais pas comment faire. J'ai vu qu'il y avait aussi des client.sock ou serveur.sock qui pouvait se créer mais pourquoi mon fichier C ne me génère pas cela ??  
 
Donc là je nage un peu après plusieurs heures de recherches.. je viens à l'aide !  
 
Merci :)
 

Code :
  1. <?php
  2. $socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
  3. socket_connect($socket, "unix://tmp/server" );
  4. $msg = "\nThis is a test\r\n";
  5. socket_write($socket, $msg, strlen($msg));
  6. socket_close($socket);
  7. ?>


 

Code :
  1. /**************************************************************************/
  2. /* This sample program provides code for aserver application that uses     */
  3. /* AF_UNIX address family                                                 */
  4. /**************************************************************************/
  5. /**************************************************************************/
  6. /* Header files needed for this sample program                            */
  7. /**************************************************************************/
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <sys/un.h>
  13. /**************************************************************************/
  14. /* Constants used by this program                                         */
  15. /**************************************************************************/
  16. #define SERVER_PATH     "/tmp/server"
  17. #define BUFFER_LENGTH    250
  18. #define FALSE              0
  19. void main()
  20. {
  21.    /***********************************************************************/
  22.    /* Variable and structure definitions.                                 */
  23.    /***********************************************************************/
  24.    int    sd=-1, sd2=-1;
  25.    int    rc, length;
  26.    char   buffer[BUFFER_LENGTH];
  27.    struct sockaddr_un serveraddr;
  28.    /***********************************************************************/
  29.    /* A do/while(FALSE) loop is used to make error cleanup easier.  The   */
  30.    /* close() of each of the socket descriptors is only done once at the  */
  31.    /* very end of the program.                                            */
  32.    /***********************************************************************/
  33.    do
  34.    {
  35.       /********************************************************************/
  36.       /* The socket() function returns a socket descriptor representing   */
  37.       /* an endpoint.  The statement also identifies that the UNIX        */
  38.       /* address family with the stream transport (SOCK_STREAM) will be   */
  39.       /* used for this socket.                                            */
  40.       /********************************************************************/
  41.       sd = socket(AF_UNIX, SOCK_STREAM, 0);
  42.       if (sd < 0)
  43.       {
  44.          perror("socket() failed" );
  45.          break;
  46.       }
  47.       /********************************************************************/
  48.       /* After the socket descriptor is created, a bind() function gets a */
  49.       /* unique name for the socket.                                      */
  50.       /********************************************************************/
  51.       memset(&serveraddr, 0, sizeof(serveraddr));
  52.       serveraddr.sun_family = AF_UNIX;
  53.       strcpy(serveraddr.sun_path, SERVER_PATH);
  54.       rc = bind(sd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
  55.       if (rc < 0)
  56.       {
  57.          perror("bind() failed" );
  58.          break;
  59.       }
  60.       /********************************************************************/
  61.       /* The listen() function allows the server to accept incoming       */
  62.       /* client connections.  In this example, the backlog is set to 10.  */
  63.       /* This means that the system will queue 10 incoming connection     */
  64.       /* requests before the system starts rejecting the incoming         */
  65.       /* requests.                                                        */
  66.       /********************************************************************/
  67.       rc = listen(sd, 10);
  68.       if (rc< 0)
  69.       {
  70.          perror("listen() failed" );
  71.          break;
  72.       }
  73.       printf("Ready for client connect().\n" );
  74.       /********************************************************************/
  75.       /* The server uses the accept() function to accept an incoming      */
  76.       /* connection request.  The accept() call will block indefinitely   */
  77.       /* waiting for the incoming connection to arrive.                   */
  78.       /********************************************************************/
  79.       sd2 = accept(sd, NULL, NULL);
  80.       if (sd2 < 0)
  81.       {
  82.          perror("accept() failed" );
  83.          break;
  84.       }
  85.       /********************************************************************/
  86.       /* In this example we know that the client will send 250 bytes of   */
  87.       /* data over.  Knowing this, we can use the SO_RCVLOWAT socket      */
  88.       /* option and specify that we don't want our recv() to wake up      */
  89.       /* until all 250 bytes of data have arrived.                        */
  90.       /********************************************************************/
  91.       length = BUFFER_LENGTH;
  92.       rc = setsockopt(sd2, SOL_SOCKET, SO_RCVLOWAT,
  93.                                           (char *)&length, sizeof(length));
  94.       if (rc < 0)
  95.       {
  96.       }
  97.       printf("%d bytes of data were received\n", rc);
  98.       if (rc == 0 ||
  99.           rc < sizeof(buffer))
  100.       {
  101.          printf("The client closed the connection before all of the\n" );
  102.          printf("data was sent\n" );
  103.          break;
  104.       }
  105.       /********************************************************************/
  106.       /* Echo the data back to the client                                 */
  107.       /********************************************************************/
  108.       rc = send(sd2, buffer, sizeof(buffer), 0);
  109.       if (rc < 0)
  110.       {
  111.          perror("send() failed" );
  112.          break;
  113.       }
  114.       /********************************************************************/
  115.       /* Program complete                                                 */
  116.       /********************************************************************/
  117.    } while (FALSE);
  118.    /***********************************************************************/
  119.    /* Close down any open socket descriptors                              */
  120.    /***********************************************************************/
  121.    if (sd != -1)
  122.       close(sd);
  123.    if (sd2 != -1)
  124.       close(sd2);
  125.     /***********************************************************************/
  126.    /* Remove the UNIX path name from the file system                      */
  127.    /***********************************************************************/
  128.    unlink(SERVER_PATH);
  129. }


Message édité par ti7bo7 le 03-10-2012 à 22:50:01
mood
Publicité
Posté le 02-10-2012 à 00:26:20  profilanswer
 

n°2158803
Farian
Posté le 02-10-2012 à 08:31:26  profilanswer
 

Bonjour !
 
Votre code C est étrange ...
 
 * Vous configurez la socket pour que chaque appel à "recv" ne rende la main qu'une fois 250 octets reçus, or, votre programme en PHP en envoie beaucoup moins que ça (17 si j'ai bien compté)
 
 * Vous ne faites pas de "recv" ! Vous n'allez donc rien recevoir !  
 
Le programme va donc tranquillement afficher "0 octets recus", puis envoyer un buffer non initialisé vers le client ... jusqu'à ce que le client ferme la socket (ce qu'il fait juste après avoir envoyé son message, donc cela devrait arriver très vite) et donc, que le "send" sorte en erreur.
 
Je pense que vous devez repenser vos programmes de tests, afin qu'ils soient cohérents :)
 
Bon courage !

n°2158844
ti7bo7
Posté le 02-10-2012 à 13:16:37  profilanswer
 

Bonjour,
 
Merci de votre réponse.
 
J'ai avancé depuis hier soir.
 
J'ai réussi à avoir une connexion et discussion mais entre deux programme C.
 
Il me reste le php qui est problématique: je ne sais pas comment écrire l'adresse du socket, certains site disent: "unix://tmp/server" mais ça ne marche pas non plus... des idées ??
 
Sachant qu'évidemment le socket est bien à cette adresse ..
 

Code :
  1. <?php
  2. $socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
  3. socket_connect($socket, "/tmp/server" );
  4. echo 'ok';
  5. $msg = "\nThis is a test\r\n";
  6. socket_write($socket, $msg, strlen($msg));
  7. socket_close($socket);
  8. ?>


 
 
Merci !


Message édité par ti7bo7 le 02-10-2012 à 16:59:01
n°2158892
czh
Posté le 02-10-2012 à 21:39:03  profilanswer
 


Code :
  1. <?php
  2. $socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
  3. var_dump($socket);
  4. $ret = socket_connect($socket, "/tmp/server" );
  5. var_dump($ret);
  6. var_dump(socket_last_error());
  7. echo 'ok';
  8. $msg = "\nThis is a test\r\n";
  9. $ret = socket_write($socket, $msg, strlen($msg));
  10. var_dump($ret);
  11. var_dump(socket_last_error());
  12. socket_close($socket);
  13. ?>


n°2159057
ti7bo7
Posté le 03-10-2012 à 22:49:27  profilanswer
 

C'est bon j'ai réussi ! merci


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

  [RESOLU] socket inter-process C/PHP linux

 

Sujets relatifs
Petit problème - langage C[PHP] FPDF error: Some data has already been output, can't send PDF fi
PHP-HTMLbesoin d'aide-language C svp
[C#] Comment faire une méthode avec un paramètre générique[Codage PHP] Affichage résumé d'une ligne
fstream / Modifier une valeur spécifique dans un fichier C++[Microsoft][C] convertir un char* en PCWSTR
Erreur de Link (Visual C++ 2005)[C#] Expression régulières
Plus de sujets relatifs à : [RESOLU] socket inter-process C/PHP linux


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