聊天軟件-管道實現

carie和vally之間的對話

talk_carie.c如下:

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

char write_fifo[] = "carie_to_vally_fifo";
char read_fifo[] = "vally_to_carie_fifo";
char writer[] = "carie";
char reader[] = "vally";

int main(int argc,char * argv[])
{
        int write_fd,read_fd;
        mkfifo(write_fifo,S_IRUSR|S_IWUSR);
        mkfifo(read_fifo,S_IRUSR|S_IWUSR);
        printf("hello,I am carie.\n");
        write_fd = open(write_fifo,O_WRONLY);
        if(write_fd < 0){
                perror("open_w");
                exit(-1);
        }
        read_fd = open(read_fifo,O_RDONLY);
        if(read_fd < 0){
                perror("open_r");
                exit(-1);
        }
        pid_t pid;
        pid = fork();
        if(pid == 0)
        {
                while(1){
                        char send[256] = "\0";
                        printf("%s:",writer);
                        fflush(stdout);
                        bzero(send,sizeof(send));
                        //scanf("%s",send);//new line separate with backspace
                        if(fgets(send,sizeof(send),stdin) == NULL){
                                exit(-1);
                        }
                        send[strlen(send)-1] = '\0';
                        write(write_fd,send,strlen(send));
                }

        }
        else if(pid > 0) //father process
        {
                while(1){
                        char recv[256] = "\0";
                        int ret;
                        ret = read(read_fd,recv,sizeof(recv));
                        if(ret <= 0){
                                exit(-1);
                        }
                        printf("\r%s:%s\n",reader,recv);
                        printf("%s:",writer);
                        fflush(stdout);
                }
        }
        return 0;
}

talk_vally.c如下:

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

char read_fifo[] = "carie_to_vally_fifo";
char write_fifo[] = "vally_to_carie_fifo";
char reader[] = "carie";
char writer[] = "vally";

int main(int argc,char * argv[])
{
        int write_fd,read_fd;
        mkfifo(write_fifo,S_IRUSR|S_IWUSR);
        mkfifo(read_fifo,S_IRUSR|S_IWUSR);
        printf("hello,I am vally.\n");
        read_fd = open(read_fifo,O_RDONLY);
        if(read_fd < 0){
                perror("open_r");
                exit(-1);
        }
        write_fd = open(write_fifo,O_WRONLY);
        if(write_fd < 0){
                perror("open_r");
                exit(-1);
        }
        pid_t pid;
        pid = fork();
        if(pid < 0){
                perror("fork");
                _exit(-1);
        }
        else if(pid == 0)
        {
                printf("in child process.\n");
                while(1){
                        char send[256] = "\0";
                        printf("%s:",writer);
                        fflush(stdout);
                        bzero(send,sizeof(send));
                        //scanf("%s",send);
                        if(fgets(send,sizeof(send),stdin) == NULL){
                                exit(-1);
                        }
                        send[strlen(send)-1] = '\0';//\n->\0
                        write(write_fd,send,strlen(send));
                }

        }
        else { //father process
                while(1){
                        char recv[256] = "\0";
                        int ret;
                        ret = read(read_fd,recv,sizeof(recv));
                        if(ret <= 0){
                                exit(-1);
                        }
                        printf("\r%s:%s\n",reader,recv);
                        printf("%s:",writer);
                        fflush(stdout);
                }
        }
        return 0;
}

運行結果如下:分別啓動talk_carie和talk_vally程序。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章