tags:

views:

264

answers:

3
$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?

+2  A: 

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);
soulmerge
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)
soulmerge
A: 

Or

socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>10, 'usec'=>0));
Milen A. Radev
it's working thank's!
A: 

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().

grantwparks