2010-11-30 29 views
1

Le serveur suivant crée un tube nommé quand il est exécuté comme ceci:Pourquoi ce tuyau nommé n'imprime-t-il pas la ligne envoyée?

./serverprogram -p nameofthepipe -t 99 

le optarg après t indique un certain nombre de threads à créer (pas fait ici).

Quoi qu'il en soit, le tuyau ne fonctionne pas ici:

/* Open the first named pipe for reading */ 
    int rdfd = open(pipeName, O_RDONLY); 

/* Read from the first pipe */ 
int numread = read(rdfd, command_and_pid, 280); 


printf("what's being read is %s \n", command_and_pid); // not printing!!1! 

Pourquoi?

programme serveur:

#include <unistd.h> 
#include <stdio.h> 
#include <errno.h> 
#include <ctype.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <string.h> 
#include <stdlib.h> 
#include <string.h> 
#include <pthread.h> 



int main (int argc, char * argv[]) 
{ 

char pipeName[30]; 
int numThreads; 

char command_and_pid[280]; 


    int opcion; 
     if (argc < 2) { 
     printf ("ERROR: Missing arguments\n");// 
     exit(1); 
    } 
    opterr = 0; 




while ((opcion = getopt (argc, argv, "p:t:w")) != -1) 
{ 
     switch (opcion) { 

       case 'p': // -p indica el nombre del pipe 
       printf("The name of the pipe is: %s\n",optarg); 
       strcpy(pipeName, optarg); 

       break; 

       case 't'://-t indica los hilos 
     printf("The number of threads is: %s\n",optarg); 
       numThreads= atoi(optarg); 

       break; 

       case '?': 
     fprintf(stderr,"no reconozco esa opcion\n"); 
       break; 
     } 
} 





    int ret_val = mkfifo(pipeName, 0666); 

    if ((ret_val == -1) && (errno != EEXIST)) { 
     perror("Error creating the named pipe"); 
     exit (0); 
    } 


    /* Open the first named pipe for reading */ 
    int rdfd = open(pipeName, O_RDONLY); 

    /* Read from the first pipe */ 
    int numread = read(rdfd, command_and_pid, 280); 


    printf("what's being read is %s \n", command_and_pid); // not printing!!1! 


    close(rdfd); 


    return 0; 
} 

programme client:

#include <unistd.h> 
#include<stdio.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <stdlib.h> 
#include <string.h> 
#include <pthread.h> 





int main (int argc, char * argv[]) 
{ 



     char pipeName[30]; 

     printf("write the name of the pipe used to write to the server \n"); 

     fgets(pipeName,30, stdin); 

     /* Open the first named pipe for writing */ 
     int wrfd = open(pipeName, O_WRONLY); 


     printf("write the name of the command you want to execute \n"); 

     char command_and_pid[280]; 
     char command[250]; 


      fgets(command,250, stdin); 
      puts(command); //quitar 

     strcpy(command_and_pid,command); 
     strcat(command_and_pid," "); 


     int pipeIntId; 

     char pidstring [30]; 

     int pid= getpid(); 

     sprintf(pidstring,"%d", pid); 

     strcat(command_and_pid,pidstring); 

     int written; 

     written=write(pipeIntId,command_and_pid,280); 
     //write to the pipe   
     // send the command and pid 


     close(pipeIntId); // close write pipe  


return 0; 
} 
+0

Est-ce que 'read()' renvoie? Quelle est la valeur de 'numread'? – chrisaycock

+0

Ce n'est pas le cas. Je suis préoccupé par le nom de pipe. Je pense que les deux extrémités de la chose ne reçoivent pas le bon nom. – andandandand

+0

Testez les valeurs de retour de 'open()' et 'read()'. – caf

Répondre

2

Dans le client, fgets maintient le saut de ligne à la fin de la ligne, de sorte que vous aurez besoin de bande qui avant d'ouvrir la fichier.

En outre, dans le code tel que donné, vous ouvrez wrfd mais en écrivant à pipeIntId, qui n'est pas initialisé (bien que peut-être vous extrayez quelque chose d'une fonction ici).

+0

ouais, j'ai attrapé ça aussi. Merci d'avoir pris le temps de regarder le code. – andandandand