Chinaunix首页 | 论坛 | 博客
  • 博客访问: 153580
  • 博文数量: 15
  • 博客积分: 2146
  • 博客等级: 大尉
  • 技术积分: 370
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-20 21:20
文章分类

全部博文(15)

文章存档

2011年(6)

2008年(9)

我的朋友

分类: LINUX

2008-07-19 14:10:50

/* fifo_cs.h */
#ifndef FIFO_CS_H
#define FIFO_CS_H

#include
#include
#include
#include
#include
#include
#include

#define MAX 25
#define SERVER_FIFO_NAME "SFIFO"
#define CLIENT_FIFO_NAME "C%d"

struct linking
{
    pid_t pid;
    char name[MAX];
};

#endif

/* fifoc.c */
#include "fifo_cs.h"

int main()
{
    int serverfd,clientfd;
    pid_t clientpid;
    char client_name[30];
    struct linking clinker,slinker;
           
    printf("Init Information\n");
    printf("The client name information\n");
    scanf("%s",clinker.name);   

    if((serverfd = open(SERVER_FIFO_NAME,O_WRONLY)) == -1)
    {
        printf("Server:can't open\n");
        exit(1);
    }       
   
    clientpid = getpid();
    clinker.pid=clientpid;
    sprintf(client_name,CLIENT_FIFO_NAME,clinker.pid);
    mkfifo(client_name,0660);
           
    if((clientfd = open(client_name,O_RDWR)) == -1)
    {
        close(serverfd);
        printf("Can't open fifo\n");
        exit(1);

    }
    write(serverfd,&clinker,sizeof(struct linking));
    read(clientfd,&slinker,sizeof(struct linking));
    printf("Client: Get server pid: %3d\n",slinker.pid);
    printf("Client: Get server name: %s\n",slinker.name);
   
    close(serverfd);
    close(clientfd);
    unlink(client_name);
    
    return 0;
}

/* fifos.c */
#include "fifo_cs.h"

int main()
{
    int serverfd,clientfd;
        pid_t serverpid;
    char client_name[30];
    struct linking slinker,clinker;

    printf("Input Information\n");
    printf("The server name information\n");
    scanf("%s",slinker.name);   

    mkfifo(SERVER_FIFO_NAME,0660);
   
    if((serverfd = open(SERVER_FIFO_NAME,O_RDWR)) == -1)
    {
        printf("Server:can't open\n");
        fflush(stdout);
        exit(1);
    }       
   
    serverpid = getpid();
    slinker.pid=serverpid;

    while(1)
    {
        read(serverfd,&clinker,sizeof(struct linking));
        sprintf(client_name,CLIENT_FIFO_NAME,clinker.pid);
        printf("Server:Get client pid: %3d\n",clinker.pid);
        printf("Server:Get client name: %s\n",clinker.name);       
       
        if((clientfd = open(client_name,O_WRONLY)) == -1)
            continue;
        write(clientfd,&slinker,sizeof(slinker));
        close(clientfd);
    }

}













阅读(1933) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~