Created
May 13, 2014 07:59
-
-
Save shonenada/319cc2297b258fc138ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <fcntl.h> | |
#include <signal.h> | |
#include "client_info.h" | |
#define FIFO_NAME "/tmp/server_fifo" | |
#define BUFF_SIZE 100 | |
char pipe_name [BUFF_SIZE]; | |
void handler (int sig) { | |
// remove pipe if signal | |
unlink(pipe_name); | |
exit(1); | |
} | |
int main (int argc, char * argv[]) { | |
int res; | |
int fifo_fd, client_fifo; | |
int fd; | |
CLIENT_INFO info; | |
char buffer[BUFF_SIZE]; | |
// handle name signals | |
signal(SIGKILL, handler); | |
signal(SIGINT, handler); | |
signal(SIGTERM, handler); | |
// check for proper command line | |
if (argc != 4) { | |
printf("Usage: %s op1 operation op2\n", argv[0]); | |
exit(1); | |
} | |
// check whether server fifo exists | |
if (access(FIFO_NAME, F_OK) == -1) { | |
printf("Could not open FIFO %s\n", FIFO_NAME); | |
exit(EXIT_FAILURE); | |
} | |
// open server FIFO for write | |
fifo_fd = open(FIFO_NAME, O_WRONLY); | |
if (fifo_fd == -1) { | |
printf("Could not open %s for write access.\n", FIFO_NAME); | |
exit(EXIT_FAILURE); | |
} | |
// create client's FIFO | |
sprintf(pipe_name, "/tmp/client_%d_fifo", getpid()); | |
res = mkfifo(pipe_name, 0777); | |
if (res != 0) { | |
printf("FIFO %s was not created!\n", buffer); | |
exit(EXIT_FAILURE); | |
} | |
// open client's FIFO for reading | |
client_fifo = open(pipe_name, O_RDONLY | O_NONBLOCK); | |
if (client_fifo == -1) { | |
printf("Could not open %s for read only access.\n", FIFO_NAME); | |
exit(EXIT_FAILURE); | |
} | |
// construct client info | |
strcpy(info.client_fifo_name, pipe_name); | |
info.lha = atoi(argv[1]); | |
info.rha = atoi(argv[3]); | |
info.op = argv[2][0]; | |
// write client info to server info | |
write(fifo_fd, &info, sizeof(CLIENT_INFO)); | |
close(fifo_fd); | |
// get result from server | |
memset(buffer, '\0', BUFF_SIZE); | |
while (1) { | |
res = read(client_fifo, buffer, BUFF_SIZE); | |
if (res > 0) { | |
printf("Received from server: %s\n", buffer); | |
break; | |
} | |
} | |
printf("Client %d is terminating\n", getpid()); | |
// delete FIFO from system | |
close(client_fifo); | |
(void) unlink(pipe_name); | |
exit(0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _CLIENTINFO_H | |
#define _CLIENTINFO_H | |
typedef struct { | |
// FIFO name of client | |
char client_fifo_name[100]; | |
// left hand argument | |
int lha; | |
// right hand argument | |
int rha; | |
// operation + - x / | |
char op; | |
} CLIENT_INFO, *CLIENT_INFO_PTR; | |
#endif // _CLIENTINFO_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <fcntl.h> | |
#include <signal.h> | |
#include "client_info.h" | |
#define FIFO_NAME "/tmp/server_fifo" | |
void handler(int sig) { | |
unlink(FIFO_NAME); | |
exit(1); | |
} | |
int calc(CLIENT_INFO_PTR info) { | |
switch (info->op) { | |
case '+': | |
return info->lha + info->rha; | |
break; | |
case '-': | |
return info->lha - info->rha; | |
break; | |
case 'x': | |
return info->lha * info->rha; | |
break; | |
case '/': | |
return info->lha / info->rha; | |
break; | |
} | |
return 0; | |
} | |
int main(int argc, char * argv[]) { | |
int i; | |
int res; | |
int fifo_fd, client_fd; | |
CLIENT_INFO info; | |
char buffer[100]; | |
signal (SIGKILL, handler); | |
signal (SIGINT, handler); | |
signal (SIGTERM, handler); | |
// create FIFO, if necessary | |
if (access(FIFO_NAME, F_OK) == -1) { | |
res = mkfifo(FIFO_NAME, 0777); | |
if (res != 0) { | |
printf("FIFO %s was not created.\n", FIFO_NAME); | |
exit(EXIT_FAILURE); | |
} | |
} | |
// open FIFO for reading | |
fifo_fd = open(FIFO_NAME, O_RDONLY); | |
if (fifo_fd == -1) { | |
printf("Could not open %s for read only access.\n", FIFO_NAME); | |
exit(EXIT_FAILURE); | |
} | |
printf("\nServer is runing!\n"); | |
while(1) { | |
res = read(fifo_fd, &info, sizeof(CLIENT_INFO)); | |
if (res != 0) { | |
printf("Client arrived!! \n"); | |
sprintf(buffer, "THe result is %d", calc(&info)); | |
client_fd = open(info.client_fifo_name, O_WRONLY | O_NONBLOCK); | |
write(client_fd, buffer, strlen(buffer) + 1); | |
close(client_fd); | |
} | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment