您好,登錄后才能下訂單哦!
server.c
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <limits.h>
- #include <string.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define MAXLINE 100
- pid_t str_handler(char*);
- void handler_client_corrupt(int);
- static char* itoa(int);//因為itoa是一個非標準C函數,因此需要自己定義
- void del_fifo(void);//退出時將管道刪除
- int main()
- {
- int fd;
- int count = 10;
- int BUF_SIZE;
- pid_t client_id;
- //注冊 程序退出時執行刪除管道
- if(atexit(del_fifo))
- {
- perror("atexit\n");
- exit(1);
- }
- #if 1
- // 創建一個客戶端到服務器共享有命管道,此管道必需不存在
- if(0 != mkfifo("../share_fifo", 0777 ))
- {
- perror("mkfifo\n");
- exit(1);
- }
- fputs("share_fifo has been created\n",stdout);
- #endif
- //打開管道并設置只讀
- fd = open("../share_fifo", O_RDONLY, S_IRUSR|S_IRGRP|S_IROTH);
- if(fd<0)
- {
- perror("open\n");
- exit(1);
- }
- //初始化緩存數組
- (MAXLINE > PIPE_BUF) ? (BUF_SIZE = PIPE_BUF) : (BUF_SIZE = MAXLINE);
- //原子操作寫進fifo最大數據量,若超過會導致進行間的竟爭 MAXLINE < PIPE_BUFPIPE_BUF = 4096
- //#define PIPE_BUF 4096
- //usr/lib/limits.h
- char buf[BUF_SIZE];
- memset(buf, sizeof(buf), 0);
- //讀取數據并進行處理
- while(20)
- {
- //讀客戶端發來的信息內容,并沖洗標準輸入出進行顯示
- int num;
- num = read(fd, buf, BUF_SIZE);
- if(-1 == num)
- {
- perror("read\n");
- exit(1);
- }
- fputs(buf,stdout);
- //由于標準輸出需要遇到換行符“\n”才會進行顯示,而收到內容中沒有換行符,使此函數進行沖洗顯示
- putchar('\n');
- //回復客戶端,并對回復的內容進行處理
- client_id = str_handler(buf);
- //處理回復內容字符串
- char str[100] = "receided successfully:";
- strcat(str,itoa(client_id));
- int len = strlen(str);
- char str_reply[len+1];
- strcpy(str_reply, str);
- //處理客戶端路徑
- char tmp[100]= "../";
- strcat(tmp,itoa(client_id));
- len = strlen(tmp);
- char path[len+1];
- strcpy(path,tmp);
- //打開對應客戶端的管道進行寫操作
- int cfd = open(path,O_WRONLY, S_IWUSR|S_IWGRP);
- if(cfd<0)
- {
- perror("open_1\n");
- exit(1);
- }
- //回復對應的客戶端
- if(write(cfd, str_reply, (strlen(str_reply)+1) ) == -1)
- {
- perror("write\n");
- exit(1);
- }
- //寫完后關閉對應的管道
- if(close(cfd))
- {
- perror("close\n");
- exit(1);
- }
- sleep(1);
- }
- if(close(fd))
- {
- perror("close\n");
- exit(1);
- }
- return 0;
- }
- pid_t str_handler(char*buf)
- {
- pid_t tmp;
- int len = strlen(buf);
- tmp = atoi(buf);
- printf("len :%d tmp:%d,buf:%s\n",len,tmp,buf);
- if( (tmp == 0) || (tmp < 0) )
- {
- return -1;
- }
- return tmp;
- }
- static char* itoa(int i)
- {
- char * local = (char*) malloc(10);
- memset(local,10,0);
- sprintf(local,"%d",i);
- return local;
- }
- void del_fifo(void)
- {
- if(remove("../share_fifo"))
- {
- perror("remove\n");
- exit(1);
- }
- }
client.c
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #define MAXLINE 100
- char buf[MAXLINE];
- char tmp[100]= "../";
- char* path;
- static char* itoa(int i)
- {
- char * local = (char*) malloc(10);
- memset(local,10,0);
- sprintf(local,"%d",i);
- return local;
- }
- void del_fifo(void)
- {
- if(remove(path))
- {
- perror("remove\n");
- exit(1);
- }
- }
- int main(int argc, char** argv)
- {
- int fd0 , fd1;
- int len;
- strcat(tmp,itoa(getpid()));
- len = strlen(tmp);
- char tmp_path[len+1];
- strcpy(tmp_path,tmp);
- path = tmp_path;
- if(0 != mkfifo(path, 0777 ))
- {
- perror("mkfifo\n");
- exit(1);
- }
- #if 0
- fd0 = open("../client0", O_RDONLY, S_IRUSR|S_IRGRP);
- if(fd0<0)
- {
- perror("open_0\n");
- exit(1);
- }
- #endif
- //處理回復內容字符串
- char str[10];
- memset(str,10,0);
- strcpy(str,itoa(getpid()));
- len = strlen(str);
- char str_send[len+1];
- strcpy(str_send, str);
- printf("%s\n",str_send);
- fd1 = open("../share_fifo", O_WRONLY, S_IWUSR|S_IWGRP);
- if(fd1<0)
- {
- perror("open_1\n");
- exit(1);
- }
- fd0 = open(path, O_RDONLY|O_NONBLOCK, S_IRUSR|S_IRGRP);
- if(fd0<0)
- {
- perror("open_0\n");
- exit(1);
- }
- int count = 10;
- while(count--)
- {
- if(write(fd1, str_send, (strlen(str_send)+1) ) == -1)
- //if(write(fd1, "test\n", 5 ) == -1)
- {
- perror("write\n");
- exit(1);
- }
- fputs("write complete\n",stdout);
- #if 0
- if(close(fd1))
- {
- perror("close\n");
- exit(1);
- }
- #endif
- while(read(fd0, buf, MAXLINE)<=0);
- printf("%s\n",buf);
- sleep(1);
- }
- return 0;
- }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。