Thread: help! fifo read problem

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    9

    help! fifo read problem

    i have a client and a server process. the server is run in the background, and waits for the client to write to a pipe (fifo). the problem is that everything that the server reads is null.

    maybe the client isn't writing, but i just don't see it.
    anyway, here's the code, if anyone can help me please, appreciate it.

    server
    Code:
    #define BUF		3
    
    //void sint(int s);
    
    char *fifo = "ru", rec[BUF+1];
    int fd, rnum, max[2], i, tempN;
    
    main(){
    
    	i = 0;
    	max[0] = 0;		//index
    	max[1] = 0;		//number
    
    	setpgid(0, 666);
    
    	//server will print after interrupt is sent
    	//signal(SIGINT, sint);
    	printf("server: now starting...\n");
    
    	//creating fifo
    	if(mkfifo(fifo, 0666) == -1)
    		perror("make fifo failed, it may alredy exist");
    	else
    		printf("server: fifo successfully created...\n");
    
    	//open for reading
    	if((fd = open(fifo, O_RDONLY)) < 0)
    		perror("fifo not opened");
    	else
    		printf("server: fifo is now open for reading...\n");
    
    	while(i < 300){
    		if(read(fd,  rec, BUF+1) == -1)
    			perror("server can't read from fifo");
    		else
    			//printf("server: reading value...\n");
    		printf("%4d -> %4d - num = %s\n", i, tempN, rec);
    		tempN = atoi(rec);
    		i++;
    
    		if(tempN > max[1]){
    			max[0] = i;
    			max[1] = tempN;
    		}
    		if(tempN > 0)
    			break;
    	}
    	close(fd);
    	exit(0);
    	//TESTING...AND IF NOT SIGNALING!!!
    	printf("======================================================\n");
    	printf("Largest value found is in position");
    	printf(" %d, and the value is %d\n", max[0], max[1]);
    	printf("======================================================\n");
    
    }
    client
    Code:
    #define BUF		3
    
    int randnum(void);
    
    main(){
    	int fd, i, rnum;
    	char *fifo = "ru", sent[BUF+1];
    
    	setpgid(0, 666);
    
    	srand((unsigned int) time(0));
    	printf("client: now starting...\n");
    	//open fifo
    	if(fd = open(fifo, O_WRONLY | O_NONBLOCK) < 0)
    		perror("client can't open fifo");
    	else
    		printf("client: fifo is now open for writing...\n");
    
    	//send message
    	for(i = 0; i <20; i++){
    		rnum = randnum();
    		sprintf(sent, "%d", rnum);
    		if(strlen(sent) > BUF)
    			printf("num too long\n");
    
    		if(write(fd,  sent, BUF+1) == -1)
    			perror("client can't write value to fifo");
    		else
    			printf("client: writing value %s...\n", sent);
    
    	}
    	printf("Client is finished sending the random values.\nExiting...\n");
    	exit(0);
    
    
    }
    
    int randnum(){
    	return   rand() % 101;
    }

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    9
    sorry, here's a sample of output

    Code:
    marko@l-190-048:~/source/ACM> s&
    server: now starting...
    make fifo failed, it may alredy exist: File exists
    [1] 1681
    marko@l-190-048:~/source/ACM> c
    client: now starting...
    client: fifo is now open for writing...
    21?client: writing value 21...
    97?client: writing value 97...
    65?client: writing value 65...
    77?client: writing value 77...
    51?client: writing value 51...
    62?client: writing value 62...
    100client: writing value 100...
    63client: writing value 63...
    38client: writing value 38...
    87client: writing value 87...
    27client: writing value 27...
    56client: writing value 56...
    26client: writing value 26...
    27client: writing value 27...
    74client: writing value 74...
    81client: writing value 81...
    47client: writing value 47...
    20client: writing value 20...
    55client: writing value 55...
    6client: writing value 6...
    Client is finished sending the random values.
    Exiting...
    server: fifo is now open for reading...
       0 ->    0 - num =
       1 ->    0 - num =
       2 ->    0 - num =
       3 ->    0 - num =
       4 ->    0 - num =
       5 ->    0 - num =
       6 ->    0 - num =
       7 ->    0 - num =
       8 ->    0 - num =
       9 ->    0 - num =
      10 ->    0 - num =
      11 ->    0 - num =
      12 ->    0 - num =
      13 ->    0 - num =
      14 ->    0 - num =
      15 ->    0 - num =
      16 ->    0 - num =
      17 ->    0 - num =
      18 ->    0 - num =
      19 ->    0 - num =
      20 ->    0 - num =
      21 ->    0 - num =
      22 ->    0 - num =
      23 ->    0 - num =
      24 ->    0 - num =
      25 ->    0 - num =
      26 ->    0 - num =
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem of read file in C++
    By rosicky2005 in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2006, 05:25 AM
  2. working with FIFO files
    By icebabe in forum C Programming
    Replies: 6
    Last Post: 05-06-2006, 11:35 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. data read problem
    By Supra in forum C Programming
    Replies: 0
    Last Post: 02-03-2002, 07:02 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM