Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of the $type parameter in socket_read()

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.

like image 418
Mike Avatar asked Jan 17 '23 05:01

Mike


2 Answers

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.

like image 171
sarnold Avatar answered Jan 20 '23 16:01

sarnold


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

like image 30
Adam Zalcman Avatar answered Jan 20 '23 15:01

Adam Zalcman