views:

2132

answers:

3

I was wondering how can I set a timeout on a socket_read call? The first time it calls socket_read, it waits till data is sent, and if no data is sent within 5 secs I want to shutdown the connection. Any Help? I already tried "SO_RCVTIMEO" with no luck.

Im creating a socket with socket_create() and listening on it for connections, then when connected I listen for the data and then do something with it. When the timeout hits, I want to run socket_shutdown() and then socket_close().

Thanks, James Hartig

A: 

http://us2.php.net/manual/en/function.stream-set-timeout.php

Steven Behnke
that doesn't work on socket_create() sockets, at least it doesn't seem to work
James Hartig
+2  A: 

Have you tried socket_set_option with SO_RCVTIMEO

Timeout value for input operations.

OIS
I tried! I think I found a solution with socket_listen and a manual timeout with time()+2 and a while.
James Hartig
Brilliant! That solved it for me. I now have a `socket_read()` that times out for me.
Jeremy Visser
+1  A: 

I did a socket_listen and then I made a manual timeout with time()+2 and a while loop with nonblock set and socket_read() inside. Seems to be working ok. Any alternatives?

UPDATE: I found that setting the socket as nonblocking and then using socket_listen provided the timeout I needed.

James Hartig
Oooh, you're accepting socket connections with PHP. That's a bit different. http://us3.php.net/manual/en/function.socket-select.php Select will return sockets if there is anything available to be read, otherwise time out with your specified timeout.
Steven Behnke