Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel a function call if it takes too long to respond in C?

Tags:

c

server

client

ftp

Is there a way to call a function, and then cancel or skip over the call if it takes too long to respond?

I'm simulating an ftp connection, and I'm receiving a response from a server using this function call:

status = receiveMessage(ccSocket, replyMsg, sizeof(replyMsg), &msgSize);

Is there a way to wait, say, 5 seconds, and skip over the function call if it has not yet responded?

like image 853
Matt123 Avatar asked Oct 27 '17 19:10

Matt123


People also ask

How do you stop a function from calling in C?

Exit() is a core function in the C/C++ programming language that is used to instantly end the calling process (function).

How do you stop a function from being called?

When you click Call the function button, it will execute the function. To stop the function, click Stop the function execution button.

How do you stop a function in C++?

In C++, you can exit a program in these ways: Call the exit function. Call the abort function. Execute a return statement from main .


1 Answers

Many different methods are available for you to solve this problem.

  1. Using a "select" call, allow your system to check if there is data available, using a given timeout which allows you to cancel the check if it takes to long.
  2. Asynchronous design enables you to "watch" a socket without ever having to "wait" on that socket, meaning your process is only ever run when it is known that data is available 3
  3. Non-Blocking design allows you to use your processor for many different reasons, where a communication on a socket should not hold your processor up, similar is a way, to Async comms, but with a small twist
like image 172
Patrick Sturm Avatar answered Sep 21 '22 09:09

Patrick Sturm