Chat application in C using named PIPES
This is a simple set of two C Programs which can act as a chat application. Basically there are two programs which need to run simultaneously. One is the client program and other the server program. This core concept used behind this is FIFO, named pipes.
Screenshots [Demo]
The Code
--> Receiver(Server) Side code
--> Sender(Client) Side code
Initially you have to start the server_chat then you can start the client_chat.
End the sentence by a "." to terminate the chat !
Found Bugs, Please Comment them here :)
Screenshots [Demo]
Client Side Chat Terminal running the client output file
Server Side Chat Terminal running the server output file
The Code
--> Receiver(Server) Side code
//client_chat #include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<unistd.h> #include<string.h> #include<linux/stat.h> #define FIFO_FILE "MYFIFO" int main() { FILE *fp; char a[40]; char readbuf[80]; start: if((fp=fopen(FIFO_FILE,"r+"))==NULL) { perror("fopen"); exit(1); } printf("Me : "); gets(a); if(a[strlen(a)-1]=='.') { fputs(a,fp); fclose(fp); return 0; } fputs(a,fp); fclose(fp); sleep(1); fp=fopen(FIFO_FILE,"r"); fgets(readbuf,80,fp); printf("Him :%s\n",readbuf); fclose(fp); sleep(1); goto start; return 0; }
--> Sender(Client) Side code
//server_chat #include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<unistd.h> #include<string.h> #include<linux/stat.h> #define FIFO_FILE "MYFIFO" int main(void) { FILE *fp; char readbuf[80],a[40]; umask(0); mknod(FIFO_FILE,S_IFIFO|0666,1); start: fp=fopen(FIFO_FILE,"r"); fgets(readbuf,80,fp); printf("Him :%s\n",readbuf); fclose(fp); sleep(1); printf("Me : "); gets(a); fp=fopen(FIFO_FILE,"r+"); if(a[strlen(a)-1]=='.') { fputs(a,fp); fclose(fp); return 0; } fputs(a,fp); fclose(fp); sleep(1); goto start; return 0; }
Initially you have to start the server_chat then you can start the client_chat.
End the sentence by a "." to terminate the chat !
Found Bugs, Please Comment them here :)
sadly they dont work....i get an segmentation fault on the client and a fopen: no such file or directory
ReplyDelete