Question

I want to set default deadline for ping in some settings file or so. My program hangs when trying to connect to non-pinging ip address.

In terminal I can call "ping 123.0.0.1 -w 5" and it wont last forever, but I can't set any deadline in code.

Was it helpful?

Solution

Shell Solution:

In your .bashrc add the following:

function ping {
    /bin/ping $@ -w5
}

This will create a wrapper function, which will set the timeout to 5 seconds for all calls to ping

Note: The version above will overwrite a -w param used on command line. If you still want to be able to overwrite the default timeout via the command line than place the -w5 before the $@:

function ping {
    /bin/ping -w5 $@
}

Pure C solution:

I won't give an full example here for brevity. You may find one here for example. In the given example, you'll have to replace the recvfrom() call which reads the ICMP response and may block by a select() or poll() call with a timeout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top