0

implementing IRC client (still). i have almost everyting but today i noticed, that there is one major problem.
i get all the data which server is sending to me, but if I do nothing but wait after displaying data it sent to me, my while(1) loop checks just once if there is any more data to receive and then stops. Starts again only if i type and send something, than after sending what i typed it checks if there s any data waiting on the socket and eventually displays it.
Which is obviously wrong, because in this situation i see messages that incame before, only if i send something.
This is my code:

while(1){    
  if((readReady = readReadiness(sockfd))==0){    //nothing to recv
          if(((writeReady = writeReadiness(sockfd))>0) && (readReady = readReadiness(sockfd))==0){  //sending is possible  
                (data sending part...)  
          }
          else
                continue; //if there s no data to read and cant send
          }
  else{         //if there s some data to recv() on the socket buffer 
          (parsing and receiving data part...)  
   } 
    continue;
}

readReadiness() and writeReadiness() use select() to check if there s any data to receive or possibility to send some.

Question is: how to make infinite loop that will repeat checking if there s any data to receive even if i do nothing ad infinitum. ? (while(1) stops checking until I send something again)
I tried to use fork(), but i dont know how to make it work and pool for data while still giving me possibility to send/receive and display it.

azrahel
  • 1,143
  • 2
  • 13
  • 31
  • When I saw the question title I though this was going to be an easy one to answer. – Dabbler May 18 '12 at 15:45
  • That''s a very twisted way of doing things. People usually loop around a _single_ select call. Looks like you could be doing up to three each time around, and the logic looks very fragile. – Mat May 18 '12 at 15:51
  • It works brilliantly in chain send-receive-send-receive and so on. so logic is not really fragile. question is how to change it or what functions to use to make it able to work like receive-receive-receive-receive-send-receive-send-receive-receive and so on... – azrahel May 18 '12 at 16:17

1 Answers1

0

In your while(1) loop, you can "poke" the server for new content regularly. The response will update the content of your socket. This would triggers that there are data to recv() on the socket buffer...

Minus
  • 729
  • 8
  • 20
  • Thought of doing so, but You admit it seems quite a barbaric solution. And first thing i thought of is - how can i poke it regularly if my while loop stops ? If i could poke it Regularly than i could regularly check if there s any data to receive as well ;) so as I see it - it solves nothing. – azrahel May 18 '12 at 16:02
  • you can use while(1=1) to insure you have an infinite loop. Your while(1) could be interpreted as do it once only. Then use sleep(time in ms). This will force the thread to wait. Then add something to the socket that will be ignored by you and check if you have some more data to show. And Yes I know this is not he best way to do it. – Minus May 18 '12 at 16:25
  • I just thought. How IRC server send data to you. Periodically or you have to ask for data? – Minus May 18 '12 at 16:27
  • It sends data if there is any to be sent :) on the server side. E.g. I join the channel (messages about channel arrives) than I see messages people are sending to each other on the channel. There isnt any regular pattern to it. – azrahel May 18 '12 at 16:33
  • correct me if am wrong but while(1) is while(true) which is the same as while(1==1) which is always true. (You meant comparison (==), not assignment(=) right ?) anyway I will check if Your suggestion works. – azrahel May 18 '12 at 16:36
  • Sorry for the mistype I did mean (==). OK, so you have to find out why the while is not looping. I suspect somewhere in the code to read data from the socket, you get stuck waiting for the buffer to get filled. So it seems to me like the while is not looping but it can't. – Minus May 18 '12 at 16:54
  • Ok ! Great, maybe I know where :)) Now if You know - how to set timer on function ? I propably got stuck at gets(sending_buffer) (waiting for me to type something to send). So how to set timer - if for lets say 5 seconds i dont type something [strlen(sending_buffer)==0 or something like that] than gets stops waiting and continue. ? – azrahel May 18 '12 at 17:05
  • this one seems better but uses Select. (http://stackoverflow.com/questions/2917881/how-to-implement-a-timeout-in-read-function-call) Or you can use something like (http://stackoverflow.com/questions/7226603/timeout-function) – Minus May 18 '12 at 18:05
  • Great Minus ! Thank You very much. Not there yet, but really getting closer and defined a problem very precisely. We are almost there (: Here s next topic of mine; ) http://stackoverflow.com/questions/10665614/how-to-loop-select-to-pool-for-data-ad-infinitum soon I am going to be master-of-network-programming...O_o – azrahel May 19 '12 at 13:37