Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP UDP socket_ recv() timeout

I am working on server/client php scripts which can communicate over a UDP socket. I was wondering if there is a good way to implement a timeout() for the socket_recv() function. Currently it is blocking in my client, waiting for an ack.

Thanks for your input!

like image 331
Philipp Werminghausen Avatar asked Dec 04 '25 00:12

Philipp Werminghausen


1 Answers

I found the solution to my question: socket_set_option() has the option SO_RCVTIMEO which set a timeout just for socket_recv()

for example:

socket_set_option($sock,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>1,"usec"=>0));

This will set a timeout on my socket on the socket level for socket_recv() for 1second and 0 microseconds http://www.php.net/manual/en/function.socket-set-option.php

Thank you if you looked for a solution!

like image 194
Philipp Werminghausen Avatar answered Dec 06 '25 14:12

Philipp Werminghausen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!