2010-11-30 16 views
1

次のサーバーは、次のように実行されるときに名前付きパイプを作成します。この名前付きパイプが送信された行を印刷しないのはなぜですか?

./serverprogram -p nameofthepipe -t 99 

t以降のoptargは、作成されるスレッドの数を示します(ここでは行われません)。

とにかく、ここでパイプが動作しません。

/* 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! 

なぜですか?

サーバープログラム:

#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; 
} 

クライアントプログラム:

#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

'read()'は返しますか? 'numread'の値は何ですか? – chrisaycock

+0

そうではありません。私はパイプ名が気になります。私はその物の両端が名前を正しく得ていないと思う。 – andandandand

+0

'open()'と 'read()'の両方の戻り値をテストします。 – caf

答えて

2

、あなたが開く前にそれを取り除く必要がありますのでfgetsは、行の末尾に改行を維持しますファイル。

また、与えられたコードでは、wrfdを開いていますが、初期化されていないpipeIntIdに書き込んでいます(関数から何かを抽出している可能性もあります)。

+0

ええ、私もそれをキャッチしました。コードを見ていただきありがとうございます。 – andandandand

関連する問題