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;
}