Find the answer to your Linux question:
Results 1 to 6 of 6
Hello everyone. i am trying to make a simple chat application using fifos. This is what i tried: PROCESS 1: Code: #include <stdio.h> #include <fcntl.h> #include <string.h> int main(void) { ...
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    5

    Simple Chat application

    Hello everyone.
    i am trying to make a simple chat application using fifos. This is what i tried:

    PROCESS 1:

    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #include <string.h>
    
    int main(void)
    {
    	int fd1, fd2;
    	char buf[100];
    
    	if( ((fd1 = open("fifo1", O_WRONLY)) < 0) || ((fd2 = open("fifo2", O_RDONLY)) < 0) ) {
    		perror("Error");
        	} else {
    		printf("\nBegin chat");		
    
    		for(;;) {
    			scanf("%s", buf);			
    			write(fd1, buf, strlen(buf) + 1);			
    
    			/* read chat reply  */				
    			for(;;) {
    				if( read(fd2, buf, 1) <= 0 ) 
    				break;
    				printf("%c", buf[0]);
    			}		
    		
    		}
    	}
    	
            close(fd1);
            close(fd2);
    		
            return 0;				
    }

    PROCESS 2:
    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #include <string.h>
    
    int main(void)
    {
    	int fd1, fd2;
    	char buf[100];
    
    	if( ((fd1 = open("fifo1", O_RDONLY)) < 0) || ((fd2 = open("fifo2", O_WRONLY)) < 0) ) {
    		perror("Error");
        	} else {
    		printf("\nBegin chat");		
    
    		for(;;) {
    			scanf("%s", buf);			
    			write(fd2, buf, strlen(buf) + 1);			
    
    			/* read chat reply from client */				
    			for(;;) {
    				if( read(fd1, buf, 1) <= 0 ) 
    				break;
    				printf("%c", buf[0]);
    			}		
    		
    		}
    	}
    	
            close(fd1);
            close(fd2);
    		
            return 0;				
    }

  2. #2
    Just Joined!
    Join Date
    Aug 2008
    Posts
    5
    But it is not working. im not able to get the changing data in either of the terminals.
    im running these processes from different terminals. But it did not work

    Can anyone please explain what changes i need to make?
    Thanks in advance.

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    53
    have a look at write and talk commands - there are several things that do this between terminals - if that's your objective.

    or is this just a programming exercise for you?

  4. #4
    Just Joined!
    Join Date
    Aug 2008
    Posts
    5
    Thanks for the reply. Yes it is just as a practice. Can you please tell me where im going wrong?

  5. #5
    Just Joined!
    Join Date
    Feb 2006
    Posts
    13
    You have to create the fifo first, use mknod to create the fifo.

    Hope this helps.

  6. #6
    Just Joined!
    Join Date
    Aug 2008
    Posts
    5
    Yes i did..But i used mkfifo.Then i added "w" permission for all.The problem is, im not able get simultaneous replies in the two terminals..ANy idea what the problem is?
    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...