Linux系统编程:客户端-服务器用FIFO进行通信

时间:2023-03-09 00:17:44
Linux系统编程:客户端-服务器用FIFO进行通信

先放下代码  回来在解释

头文件:

clientinfo.h

 struct CLIENTINFO{
char myfifo[];
int leftarg;
int rightarg;
char op;
};
typedef struct CLIENTINFO CLIENTINFO, *CLINTINFOPTR;

client.c

 #include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include "clientinfo.h" #define FIFO_NAME "/home/levi/chatapplication/data/server_fifo/chat_server_fifo"
#define BUFF_SZ 100 char mypipename[BUFF_SZ];
void handler(int sig){
unlink(mypipename);
exit();
} int main(int argc, char const *argv[])
{
int res;
int fifo_fd, my_fifo;
int fd;
CLIENTINFO info;
char buffer[BUFF_SZ]; signal(SIGKILL, handler);
signal(SIGINT, handler);
signal(SIGTERM, handler); if(argc != ){
printf("Usage: %s op1 operation op2\n", argv[]);
exit();
}
if(access(FIFO_NAME, F_OK) == -){
printf("Could not open FIFO %s\n", FIFO_NAME);
exit(EXIT_FAILURE);
}
fifo_fd = open(FIFO_NAME, O_WRONLY);
if(fifo_fd == -){
printf("Could not open %s for write access\n", FIFO_NAME);
exit(EXIT_FAILURE);
}
sprintf(mypipename, "/home/levi/chat_client_%d_fifo", getpid());
res = mkfifo(mypipename, );
if(res != ){
printf("FIFO %s was not created\n", buffer);
exit(EXIT_FAILURE);
} my_fifo = open(mypipename, O_RDONLY | O_NONBLOCK);
if(my_fifo == -){
printf("Could not open %s for read only access\n", FIFO_NAME);
exit(EXIT_FAILURE);
} strcpy(info.myfifo, mypipename);
info.leftarg = atoi(argv[]);
info.op = argv[][];
info.rightarg = atoi(argv[]); write(fifo_fd, &info, sizeof(CLIENTINFO));
close(fifo_fd); memset(buffer, '\0', BUFF_SZ);
while(){
res = read(my_fifo, buffer, BUFF_SZ);
if(res > ){
printf("Received from server : %s\n", buffer);
break;
}
}
printf("Client %d is terminating\n", getpid());
close(my_fifo);
(void)unlink(mypipename);
return ;
}

server.c

 #include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include "clientinfo.h" #define FIFO_NAME "/home/levi/server_fifo/chat_server_fifo"
void handler(int sig){
unlink(FIFO_NAME);
exit();
} int calc(CLIENTINFOPTR info){
switch(info->op){
case '+' : return info->leftarg + info->rightarg;
case '-' : return info->leftarg - info->rightarg;
case '*' : return info->leftarg * info->rightarg;
case '/' : return info->leftarg / info->rightarg;
}
return ;
} int main(int argc, char const *argv[])
{
int res, i, fifo_fd, fd1;
CLIENTINFO info;
char buffer[];
signal(SIGKILL, handler);
signal(SIGINT, handler);
signal(SIGTERM, handler);
if(access(FIFO_NAME, F_OK) == -){
res = mkfifo(FIFO_NAME, );
if(res != ){
printf("FIFO %s was not created\n", FIFO_NAME);
exit(EXIT_FAILURE);
}
}
fifo_fd = open(FIFO_NAME, O_RDONLY);
if(fifo_fd == -){
printf("Could not open %s for read only access\n", FIFO_NAME);
exit(EXIT_FAILURE);
}
printf("\nServer is rarin to go !\n");
while(){
res = read(fifo_fd, &info, sizeof(CLIENTINFO));
if(res != ){
printf("Client arrived!!\n");
sprintf(buffer, "The result is %d", calc(&info));
fd1 = open(info.myfifo, O_WRONLY | O_NONBLOCK);
write (fd1, buffer, strlen(buffer) + );
close(fd1);
} }
return ;
}

OK!

./server.c &

./client 3 + 5

  看输出吧!