Basically I have a server running an almost infinite, select controlled, mini webserver. I have created a fifo file with mkfifo called "pipe" which is opened in read only mode. My select is suppose to wait for something to happen in that file (which talks to a java GUI using JNI but not important) or if a client connects asking for a website. The GUI has pause, unpause and shutdown options so I figured I would have a variable "paused" in my server which if paused would stop the "FD_SET(s_sock, &fs)" with an if statement. The server will wait perfectly until I send it something along the pipe (the first time) then the select picks it out my "readGUI()" function reads the message and returns; 1, if there was nothing in the pipe or an unpaused call; 0, if there was a pause call; and -1, if there was a shutdown call. My problem is that after it goes back and initializes the select variables again the select automatically calls my readGUI again, says theres nothing in there, returns a 1 and continues this loop indefinetly.
This would have to be a problem with my select I would assume because it proves my pipe is empty.
Heres the code:
Code:
...
shutter_down = 1;
paused = 1;
while(shutter_down)
{
T.tv_sec = 1;
T.tv_usec = 0;
FD_ZERO(&fs);
FD_SET(fd, &fs);
if (paused)
FD_SET(s_sock, &fs);
r = select(FD_SETSIZE, &fs, NULL, NULL, &T);
if (r < 0)
exit (-1)
else if (r > 0)
{
if (FD_ISSET(fd, &fs))
{
paused = readGUI();
if (paused == -1)
shutter_down = 0;
}
else if (FD_ISSET(s_sock, &fs))
{
/*Accept incoming socket and do stuff*/
}
}
}
int readGUI()
{
char buf[100];
int ret_val;
ret_val = read(fd, buf, sizeof(buf));
if (ret_val < 1)
return 1; /*Nothings there, big problem*/
buf[ret_val] = '\0';
printf ("buf: %s\n", buf); /*This only gets printed the first time*/
/*Its how I know theres nothing in the pipe*/
if (!(strcasecmp(buf, "1")))
return 1;
if (!(strcasecmp(buf, "0")))
return 0;
if (!(strcasecmp(buf, "-1")))
return -1;
return 1;
}
Let me know if you need more code. Only posted the problem.
Thanks in advance.