/* 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) |