In order to build a UDP chat, I need to send a message and at the same time, receive messages. For the moment, I am trying to receive messages and to read the command line, print the message. Moreover, I want to call a function func() every 10 seconds.

This is what I have tried:

  fd_set readfds;
  struct timeval timeout;
  char buf[100];

  while(1){
    FD_ZERO(&readfds);
    FD_SET(sock_fd,&readfds);

    timeout.tv_sec = 10;
    timeout.tv_usec = 0;

    if(select(sock_fd +1, &readfds,NULL,NULL,&timeout) != 0){
        receiveMessages(sock_fd);
        memset((void*)buf,0,100);
        if(read(sock_fd,(void*)buf,100)!=-1){
          printf("Message : %s\n", buf );
        }
    }else{
        func();
    }
 }

In the receiveMessages()function, only one function is called: recvfrom().

The functions receiveMessages(sock_fd) and func()are correctly called every 10 seconds but the command line is never read, so the message is never printed. What did I miss ?

0 Answers