0

I writing php script that communicates with the server over unix local sockets. 1) connect 2) send msg with socket_write() (90% of time low-size blocks, 10% big size) 3) get the answer 4) close connection

Now, I have simple socket_write() implementation like suggested at comments from php manual "jean at briskula dot si 03-Feb-2011 03:00"

Seems its work fine on blocking mode, but I want to implement timeouts for socket_write, therefore I think (need) to use non-blocking mode, when socket_write() called.

Is there any advantages of blocking mode over non-blocking? Is there a reason to realize timeouts? How to realize them correctly? Any examples?

Tried to write on a while circle, but got ===FALSE, maybe need to socket_select() every time before socket_write() called?

ps. this question also actual for socket_read() when reading answer from server

abrahab
  • 2,430
  • 9
  • 39
  • 64

1 Answers1

1

For a PHP (web-facing) script sending data to/receiving data from a backend system through sockets without the chance for other processing in the meantime (which is basically why you'd want to use non-blocking mode in the first place), there's no sense in handling the complications that non-blocking mode causes.

So, stick with blocking mode.

modelnine
  • 1,499
  • 8
  • 11
  • thanks, but what i can do with situations that can occurs while requests will go to long (infinitely) for some reason and this will block thread of c++ server? i feel that somewhere need to add timeouts... maybe at c++ side, but how? SO_LINGER? – abrahab Apr 12 '12 at 11:18
  • 1
    If your worry is the C++-side, use non-blocking sockets there to query data, implementing some form of timeout. This answer just refers to the PHP-facing side of the equation. For the (proper) usage of non-blocking I/O, check out http://stackoverflow.com/questions/6715736/using-select-for-non-blocking-sockets for example. – modelnine Apr 12 '12 at 13:32
  • thanks, still leave it as is for now, because solution with select() is not nice and need to rewrite all threads logic at the existing program. maybe at the feature will be read something about whole socket timeouts. in my opinion SO_LINGER is responsible for that. and thread with freezed recv or send will never blocks forever. – abrahab Apr 12 '12 at 18:41