Linux下利用FIFO+Thread实现聊天

/*client1_str.c*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>

char name[10];
typedef struct
{
    int  fd[2];
    char timeCur[20];
    char name[20];

}clientInfo;


void getCurTime(char timeCur[])
{
    time_t  Time;
    struct tm* Tm = NULL;

    time(&Time);
    Tm = localtime(&Time);
    strftime(timeCur,20,"%Y/%m/%d %H:%M:%S",Tm);

}

void *writeFifo(void *arg)
{
    clientInfo * Client = (clientInfo *)arg;
    int fd = Client->fd[0];
    char buf[50];
    char messages[50];

    while(1)
    {
        memset(messages,0,sizeof(messages));

        int len = 0;
        len = read(STDIN_FILENO,messages,sizeof(messages));
        if(len > 0)
        {
            write(fd, messages, sizeof(messages));
        }
        else
        {
            printf("writeFifo failed %s",strerror(errno));
            break;
        }

    }

    close(fd);
    return NULL;
}


void *readFifo(void *arg)
{
    clientInfo * Client = (clientInfo *)arg;
    int fd = Client->fd[1];
    char buf[50];

    while(1)
    {
        memset(buf,0,sizeof(buf));  
        int len = 0;
        len = read(fd, buf, sizeof(buf));

        if(len > 0)
        {
            write(STDOUT_FILENO,buf,sizeof(buf));

        }
        else 
        {
            break;
        }      

    }

    close(fd);
    return NULL;
}

void authorInfo()
{
    puts("============================================");
    puts("Name        :ICQ in Linux");
    puts("Version     :v1.0 Beta ");
    puts("Copyright   :Power by x_jwei");
    puts("Description :A multithreaded programming in C,Ansi-style");
    puts("============================================");
}

int main(int argc,char *argv[])
{
    if(argc < 2)
    {
        printf("Usage: %s <Your Name>\n",argv[0]);
        return -1;
    }

    int fd0 = -1,fd1 = -1;

    if ( (fd0 = open("fifo0",O_RDONLY)) == -1 )
    {

        printf("open fifo0 error %s",strerror(errno));
        return -1;
    }

    if (  (fd1 = open("fifo1",O_WRONLY)) == -1  )
    {

        printf("open fifo1 error %s",strerror(errno));
        return -1;
    }

    clientInfo *client;
    client = (clientInfo *)malloc(sizeof(clientInfo));
    memset(client,0,sizeof(clientInfo));

    client->fd[0] = fd1;
    client->fd[1] = fd0;
    strcpy(client->name,argv[1]);

    pthread_t thr1,thr2;
    pthread_create(&thr1,NULL,writeFifo,client);
    pthread_create(&thr2,NULL,readFifo,client);
    pthread_join(thr1, NULL);
    pthread_join(thr2, NULL);


    return 0;
}




/*client2_str.c*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>

char name[10];
typedef struct
{
    int  fd[2];
    char timeCur[20];
    char name[20];

}clientInfo;


void getCurTime(char timeCur[])
{
    time_t  Time;
    struct tm* Tm = NULL;

    time(&Time);
    Tm = localtime(&Time);
    strftime(timeCur,20,"%Y/%m/%d %H:%M:%S",Tm);

}

void *writeFifo(void *arg)
{
    clientInfo * Client = (clientInfo *)arg;
    int fd = Client->fd[0];
    char buf[50];
    char messages[50];

    while(1)
    {
        memset(messages,0,sizeof(messages));

        int len = 0;
        len = read(STDIN_FILENO,messages,sizeof(messages));
        if(len > 0)
        {
            write(fd, messages, sizeof(messages));
        }
        else
        {
            printf("writeFifo failed %s",strerror(errno));
            break;
        }

    }

    close(fd);
    return NULL;
}


void *readFifo(void *arg)
{
    clientInfo * Client = (clientInfo *)arg;
    int fd = Client->fd[1];
    char buf[50];

    while(1)
    {
        memset(buf,0,sizeof(buf));  
        int len = 0;
        len = read(fd, buf, sizeof(buf));

        if(len > 0)
        {
            write(STDOUT_FILENO,buf,sizeof(buf));   
        }
        else 
        {
            break;
        }      

    }

    close(fd);
    return NULL;
}

void authorInfo()
{
    puts("============================================");
    puts("Name        :ICQ in Linux");
    puts("Version     :v1.0 Beta ");
    puts("Copyright   :Power by x_jwei");
    puts("Description :A multithreaded programming in C,Ansi-style");
    puts("============================================");
}

int main(int argc,char *argv[])
{
    if(argc < 2)
    {
        printf("Usage: %s <Your Name>\n",argv[0]);
        return -1;
    }

    int fd0 = -1,fd1 = -1;

    if (  ( fd0 = open("fifo0",O_WRONLY)) == -1 )
    {

        printf("open fifo0 error %s",strerror(errno));
        return -1;
    }

    if( (fd1 = open("fifo1",O_RDONLY)) == -1  )
    {

        printf("open fifo1 error %s",strerror(errno));
        return -1;
    }

    clientInfo *client;
    client = (clientInfo *)malloc(sizeof(clientInfo));
    memset(client,0,sizeof(clientInfo));

    client->fd[0] = fd0;
    client->fd[1] = fd1;
    strcpy(client->name,argv[1]);

    pthread_t thr1,thr2;
    pthread_create(&thr1,NULL,writeFifo,client);
    pthread_create(&thr2,NULL,readFifo,client);
    pthread_join(thr1, NULL);
    pthread_join(thr2, NULL);


    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少言才不会咸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值