/*************************************************************************
> File Name: client.h
> Description: client.h定义了客户和服务器程序都会用到的数据.
> Author: Liubingbing
> Created Time: 2015年07月15日 星期三 22时21分46秒
> Other: client.h
************************************************************************/
#ifndef _CLIENT_H
#define _CLIENT_H
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#define SERVER_FIFO_NAME "/tmp/serv_fifo"
#define CLIENT_FIFO_NAME "/tmp/cli_%d_fifo"
#define BUFFER_SIZE 20
struct data_to_pass_st {
pid_t client_pid;
char some_data[BUFFER_SIZE - 1];
};
编写服务器程序server.c,它创建并打开服务器管道,设置为只读的阻塞模式。接着,服务器开始读取客户发送来的数据,这些数据采用的data_to_pass_st结构。/*************************************************************************
> File Name: server.c
> Description: server.c程序创建并打开服务器管道,设置为只读的阻塞模式.
> Author: Liubingbing
> Created Time: 2015年07月15日 星期三 22时22分49秒
> Other: server.c
************************************************************************/
#include "client.h"
#include <ctype.h>
int main()
{
int server_fifo_fd, client_fifo_fd;
struct data_to_pass_st my_data;
int read_res;
char client_fifo[256];
char *tmp_char_ptr;
/* mkfifo函数创建命名管道(即特殊类型的文件SERVER_FIFO_NAME) */
mkfifo(SERVER_FIFO_NAME, 0777);
/* open函数打开SERVER_FIFO_NAME文件,设置为O_RDONLY的阻塞模式
* 如果成功,则返回文件描述符 */
server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY);
if (server_fifo_fd == -1) {
fprintf(stderr, "Server fifo failure\n");
exit(EXIT_FAILURE);
}
sleep(10);
do {
/* read函数从server_fifo_)fd指向的文件读入sizeof(my_data)个字节的数据到my_data指向的内存
* 如果成功,则返回实际读入数据的字节数 */
read_res = read(server_fifo_fd, &my_data, sizeof(my_data));
if (read_res > 0) {
/* 对刚从客户那里读到的数据进行处理,把some_data中的所有字符全部转换为大写*/
tmp_char_ptr = my_data.some_data;
while (*tmp_char_ptr) {
*tmp_char_ptr = toupper(*tmp_char_ptr);
tmp_char_ptr++;
}
/* sprintf把格式化的数据写入到client_fifo指向的内存中 */
sprintf(client_fifo, CLIENT_FIFO_NAME, my_data.client_pid);
/* open函数打开文件client_fifo,以O_WRONLY只写的阻塞模式 */
client_fifo_fd = open(client_fifo, O_WRONLY);
if (client_fifo_fd != -1) {
/* write函数从my_data指向的内存写入sizeof(my_data)个字节的数据到client_fifo_fd指向的文件中
* 如果成功,返回实际写入数据的字节数 */
write(client_fifo_fd, &my_data, sizeof(my_data));
close(client_fifo_fd);
}
}
} while (read_res > 0);
close(server_fifo_fd);
/* 关闭服务器管道的文件描述符,删除FIFO文件 */
unlink(SERVER_FIFO_NAME);
exit(EXIT_SUCCESS);
}编写客户程序client.c,这个程序的第一部分先检查服务器FIFO文件是否存在,如果存在就打开它,然后它获取自己的进程ID,该进程ID构成要发送给服务器的数据的一部分。接下来,它创建客户FIFO,为下一部分内容做好准备。/*************************************************************************
> File Name: client.c
> Description: client.c程序先检查服务器FIFO文件是否存在,如果存在就打开,然后获取自己的进程ID,该进程ID构成要发送给服务器的数据的一部分.
> Author: Liubingbing
> Created Time: 2015年07月15日 星期三 22时23分55秒
> Other: client.c
************************************************************************/
#include <stdio.h>
#include "client.h"
#include <ctype.h>
int main()
{
int server_fifo_fd, client_fifo_fd;
struct data_to_pass_st my_data;
int times_to_send;
char client_fifo[256];
/* open函数打开文件SERVER_FIFO_NAME,设置为O_WRONLY的阻塞模式
* 如果成功,则返回文件描述符 */
server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);
if (server_fifo_fd == -1) {
fprintf(stderr, "Sorry, no server\n");
exit(EXIT_FAILURE);
}
/* 获得进程ID */
my_data.client_pid = getpid();
/* sprintf函数写入格式的数据到client_fifo指向的内存中 */
sprintf(client_fifo, CLIENT_FIFO_NAME, my_data.client_pid);
if (mkfifo(client_fifo, 0777) == -1) {
fprintf(stderr, "Sorry, can't make %s\n", client_fifo);
exit(EXIT_FAILURE);
}
for (times_to_send = 0; times_to_send < 5; times_to_send++) {
sprintf(my_data.some_data, "hello from %d", my_data.client_pid);
printf("%d sent %s, ", my_data.client_pid, my_data.some_data);
write(server_fifo_fd, &my_data, sizeof(my_data));
client_fifo_fd = open(client_fifo, O_RDONLY);
if (client_fifo_fd != -1) {
if (read(client_fifo_fd, &my_data, sizeof(my_data)) > 0) {
printf("reveived: %s\n", my_data.some_data);
}
close(client_fifo_fd);
}
}
close(server_fifo_fd);
unlink(client_fifo);
exit(EXIT_SUCCESS);
}版权声明:本文为博主原创文章,未经博主允许不得转载。
linux程序设计——使用FIFO的客户/服务器的应用程序(第十三章)
原文:http://blog.csdn.net/yiranant/article/details/46900961