4

I try to create a tcp/ip socket connection from a c# app to a PHP 5.3 script using PHP sockets. The c# app should send JSON strings to the PHP script.

My question in regard to the socket_read manual: What do they mean with:

"PHP_BINARY_READ (Default) - use the system recv() function.
Safe for reading binary data."

What exactly does PHP_BINARY_READ and why should I use the recv() function when using this parameter?

Any help is highly appreciated.

hakre
  • 193,403
  • 52
  • 435
  • 836
Mike
  • 1,992
  • 4
  • 31
  • 42

2 Answers2

5

The important part is what the documentation says about the other choice:

  • PHP_NORMAL_READ - reading stops at \n or \r.

Pick PHP_NORMAL_READ if your socket is a line-oriented text protocol. Pick PHP_BINARY_READ if your socket is anything else.

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • 3
    Ah! That's just a misunderstanding of their English; that is a statement that when you give that parameter, it is a command to PHP that it should use the underlying `recv()` system call to receive the data. _You_ don't have to do anything about it; it is again, a contrast to the higher-level line-oriented workings of the other parameter. – sarnold Mar 15 '12 at 23:40
  • 1
    aha, got it! :-) I was confused because such a function actually exists in PHP: http://www.php.net/manual/en/function.socket-recv.php – Mike Mar 15 '12 at 23:49
4

This means that when you use PHP_BINARY_READ then this system call will be used to read from the underlying socket. The remark about safety for binary data is explained by contrasting this with the alternative which reads

PHP_NORMAL_READ - reading stops at \n or \r.

Hence, if you would like to read one line at a time, then use PHP_NORMAL_READ. Otherwise, use PHP_BINARY_READ (which is default).

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92