C sockets close connection if no response -


i have small problem waiting response client. code looks this:

    num_bytes_received = recv(recvfd, line, max_line_size-1, 0);      if(line[0] == 'r')     {         do_something();     }      if(line[0] == 'p')     {         do_another_thing();     } 

is there simple way wait message let's 30 seconds , if there's no message execute do_another_thing(); function? it's not connection problems situation (like clients disconnect etc.). it's own limitation create.

you can use select() timeout.

int ret; fd_set set; struct timeval timeout; /* initialize file descriptor set. */ fd_zero(&set); fd_set(recvfd, &set);  /* initialize timeout data structure. */ timeout.tv_sec = 30; timeout.tv_usec = 0;  /* select returns 0 if timeout, 1 if input available, -1 if error. */ ret = select(recvfd+1, &set, null, null, &timeout)); if (ret == 1) {     num_bytes_received = recv(recvfd, line, max_line_size-1, 0);     if(line[0] == 'r')     {         do_something();     }      if(line[0] == 'p')     {         do_another_thing();     } }  else if (ret == 0) {     /* timeout */     do_another_thing(); } else {     /* error handling */ } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -