Chinaunix

标题: 命名管道[已经解决] [打印本页]

作者: lnfxcf    时间: 2007-05-17 13:19
标题: 命名管道[已经解决]
Server 端代码
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#include <linux/stat.h>

#define FIFO_FILE "MYFIFO"

int main(void)
{
        FILE *fp;
        char readbuf[80];

        printf("Waiting message...\n");
        umask(0);
        mknod(FIFO_FILE, S_IFIFO|0666, 0);
        while(1)
        {
                fp = fopen(FIFO_FILE, "r");
                fgets(readbuf, 80, fp);
                printf("Receiveed string :%s", readbuf);
                fclose(fp);
                sleep(1);
        }

        return 0;

}


Client 端代码
#include <stdlib.h>
#include <stdio.h>

#define FIFO_FILE "MYFIFO"

int main(int argc, char *argv[])
{
        FILE *fp;
        if(argc != 2)
        {
                printf("Usage: fifoclient [string]\n");
                exit(1);
        }
        if((fp = fopen(FIFO_FILE, "w")) == NULL)
        {
                perror("fopen");
                exit(1);
        }
        fputs(argv[1], fp);
        fclose(fp);
        return 0;
}

为啥client端执行之后server端没有任何反应呢?

[ 本帖最后由 lnfxcf 于 2007-5-17 14:48 编辑 ]
作者: samuel1004    时间: 2007-05-17 13:43
是不是该用open read 之类的系统调用,我记得我写的时候都是用的这些系统调用
作者: emacsnw    时间: 2007-05-17 14:31
Buffer...

在 printf("Receiveed string :%s", readbuf); 后面加一句 fflush(stdout);
作者: lnfxcf    时间: 2007-05-17 14:48
哦!非常感谢!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2