ZODIACK
ZODIACK

Reputation: 23

What happens after setsockpt timeval? And how to handle the timeout event? How to get back to the same line of code that was executing before timeout?

When the software is hit by a signal (example: SIGINT), if you previously used signal(SIGINT, SIGINT_HANDLER) (yea, I know signal is evil and all, but just to give the idea) then the software will react this way:

  1. the routine f is hit by SIGINT
  2. the routine f stops its execution
  3. control is given to another routine, g, which is SIGINT_HANDLER that will do stuff
  4. After g finishes, control is given back to routine f which will resume the execution where it stopped

Ok. I need something like this, but not with signals. In particular:

I have the client socket, which I declered with setsockopt and gave it 4 minutes time. If, after 4 minutes, there's nothing on the socket, then the process shall take back control. Well, there's a problem.

Imagine this:

struct timeval tv;
tv.tv_sec = 240;  // 4 minutes timeout;
if (setsockopt(my_socket, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval))) {
   perror("setsockopt: rcvtimeo");
   exit(1);
}

/* I've seen another stack answer where someone suggested this as the correct way to set a timeout. If there's anything wrong with it, please let me know! */

// The user must write a mex on the socket. I know scanf is inappropriate, but it's just to make it simple 
char buff[4096];
fgets("%s", buff);

write(my_socket, buff, strlen(buff) // Yes, in my code I check for errors on write;

Ok, now the client wants a coffee, leaves the room and the client machine is waiting for something to put on the socket. Well, 1,2,3,4 minutes pass.

Now, notice, the client is stuck in the scanf function (or fgets, because that's what I should use) when the timeout hits. Therefore, what happens? I don't have the slightest idea. Because my teacher's slide say "the process takes control back". Fine, but what does it mean?

The reason why I talked about signals before is to know if there's like a way to tell the software this:

  1. There was a timeout;
  2. pass control to a g routine, which is a routine used to handle the timeout event;
  3. When here, spawn a new terminal window and display "press any key to keep connection alive" (I don't know how to spawn and delete terminal windows from C, but I'll ask in another question);
  4. If the user, in the span of a minute (because, after a minute, the server will close the socket and the client is kicked out of the system) enters any key, then send the mex "KEEP_ALIVE" to server.
  5. When server answers with "KEEP_ALIVE_OK" close the spawned terminal window
  6. Give control back to previous routine. Notice: when the timeout occurred, client was writing in scanf, and I'd like to resume where he left

I know it's very difficult, but is there a way to do this in C?

My professor wants a very robust architecture, but this stuff is quite advanced and I don't know where to look for information.

Upvotes: 0

Views: 349

Answers (1)

Nothing happens. SO_RCVTIMEO sets how long it will be, when you call recv (or any of the variations like recvfrom or recvmsg - or read), before it returns a timeout error.

You don't call recv, so you don't get any timeout error.

Even if you did call recv, this has nothing to do with signals. It doesn't cause recv to raise a signal after 4 minutes. It just causes recv to return an error code if it's still waiting after 4 minutes. It doesn't even mess up the socket - you can call recv again after that, and it will wait some more.


Programs that handle multiple input sources and timing usually use poll (or one of its variants or select). This is a function that lets you wait for several things at the same time, so a poll call can wait for the socket to receive data or for the user to type something or for 4 minutes to be up. You didn't actually ask about this, so I haven't explained it any further.

Upvotes: 1

Related Questions