2011-10-30 14 views
0

私はこの作業をしようとしていますが、運がない、基本的に私はパイプに書き込んだ後、パイプに戻って私が送信したテキストを返す必要があります。私はserver.cとclient.cを持っているので、server.cを実行させる...新しいターミナルを開いてクライアントを実行する。問題は、クライアントがそれを実行するときに何もしないということです。パイプを閉じるのと同じように何かが欠けていると確信しています。私はわからない..私は本当にいくつかのガイダンスパイプで作業する

server.c

#include <stdio.h> 
#include <errno.h> 
#include <ctype.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <string.h> 
#define PIPE1  "PIPE1" 
#define PIPE5  "PIPE5" 

#define MAX_BUF_SIZE 255 

int main(int argc, char *argv[]) 
{ 
    int rdfd1,rdfd2,rdfd3,rdfd4, wrfd1,wrfd2,wrfd3,wrfd4,ret_val, count, numread1,numread2,numread3,numread4; 
    char buf1[MAX_BUF_SIZE]; 
    char buf2[MAX_BUF_SIZE]; 
    char buf3[MAX_BUF_SIZE]; 
    char buf4[MAX_BUF_SIZE]; 

    /* Create the first named - pipe */ 
    ret_val = mkfifo(PIPE1, 0666); 


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



    ret_val = mkfifo(PIPE5, 0666); 

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

    /* Open the first named pipe for reading */ 
    rdfd1 = open(PIPE1, O_RDONLY); 


    /* Open the first named pipe for writing */ 
    wrfd1 = open(PIPE5, O_WRONLY); 


    /* Read from the pipes */ 
    numread1 = read(rdfd1, buf1, MAX_BUF_SIZE); 


    buf1[numread1] = '0'; 

    printf("Server : Read From the pipe : %sn", buf1); 


    /* 
    * Write the converted content to 
    * pipe 
    */  
    write(wrfd1, buf1, strlen(buf1)); 

} 

いるclient.c

#include <errno.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 
#include <ctype.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#define PIPE1  "PIPE1" 
#define PIPE5  "PIPE5" 


#define MAX_BUF_SIZE 255 


int main(int argc, char *argv[ ]) { 
    pid_t childpid;    
    int error;     
    int i;      
    int nprocs;     
      /* check command line for a valid number of processes to generate */ 

    int wrfd1, rdfd1, numread; 
    char rdbuf[MAX_BUF_SIZE]; 

    if ((argc != 2) || ((nprocs = atoi (argv[1])) <= 0)) { 
     fprintf (stderr, "Usage: %s nprocs\n", argv[0]); 
     return 1; 
    } 



    for (i = 1; i < nprocs; i++) { 
     /* create the remaining processes */ 

      if ((childpid = fork()) == -1) { 
        fprintf(stderr, "[%ld]:failed to create child %d: %s\n", (long)getpid(), i, strerror(errno)); 
        return 1; 
       } 

      /* Open the first named pipe for writing */ 
      wrfd1 = open(PIPE5, O_WRONLY); 

      /* Open the second named pipe for reading */ 
     rdfd1 = open(PIPE1, O_RDONLY); 


     if (childpid) 
     break; 

      char string1[100]; 

      if(sprintf(string1, "This is process %d with ID %ld and parent id %ld\n", i, (long)getpid(), (long)getppid())) { 
       write(wrfd1,string1, strlen(string1)); 
       } 

     /* Read from the pipe */ 
     numread = read(rdfd1, rdbuf, MAX_BUF_SIZE); 

     rdbuf[numread] = '0'; 

     printf("Full Duplex Client : Read From the Pipe : %sn", rdbuf);  
     } 


    return 0; 
} 
+0

誤ったコードを編集または削除しないでください。あなたがそれを取り除くと、反応と答えが文脈を失います。 – wildplasser

答えて

2

サーバーとクライアントの両方がPIPE1から読み取り、PIPE5に書き込むようです。他の人がPIPE1に書き込むべきではないので、もう片方がそれを相手側から読むことができますか?

また、./client 1でテストしている場合、for (i = 1; i < nprocs; i++)ループは実行されません。

最後にもう1つ、このquestionを参照してください。私はそれがあなたのコードに当てはまるかどうか完全には分かっていませんが、心に留めておく価値があります。

+0

はい、それはアイデアです、多分私のロジックは間違っています..どこに問題が見えますか? – user975582

+0

ええ、私はそれらを切り替えますが、依然としてクライアントは何もしません。 – user975582

1

この行はすべきではない'\0'をいただければ幸いですか!

buf1[numread1] = '0'; 
関連する問題