0
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, $ip_server , $port);

socket_sendto($socket, $ascii_egyben_kimenet, strlen($ascii_egyben_kimenet), 0, $ip_plc , $port);
$valasz_kimenet=socket_read($socket, 256);

Because the socket_read the server is waiting for an answer... How I can define the max time to wait?

3 Answers 3

2

You do that with the function socket_set_timeout(). Example for a 1/2 second timeout:

socket_set_timeout($socket, 0, 500);

You can check if someone is trying to connect without blocking by setting the socket to non-blocking mode:

socket_set_blocking($socket, 0);
Sign up to request clarification or add additional context in comments.

2 Comments

socket_set_timeout($socket, 3, 500); where to put it? I tried but not working.
You should set the timeout value before you call socket_read(). If it really does not work, you should enable error_reporting(E_ALL)
1

Or

socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>10, 'usec'=>0));

Comments

1

I don't know if the same problem shows up with socket_set_timeout(), but with serious use of stream_set_timeout(), I found that PHP was setting the time limit at double what I specified. So if I told it 60 seconds, it actually was exactly two minutes before I stopped listening. I actually had to take the number of seconds desired and multiply by 500000 to get the value to use in stream_set_timeout().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.