PHP socket_recv and socket_read

From reading the PHP manual, the function socket_recv and socket_read look the same for me, both functions receive data from the client.

can someone tell me what distinguishes these two functions?

+4
source share
3 answers

socket_recv returns the number of bytes received socket_read returns the data that was received

With socket_recv you can read bytes from the buffer and know how many bytes were received. With socket_read you can only read a specific amount of data from a buffer

+8
source

From http://www.faqs.org/faqs/unix-faq/socket/#b :

2.18. What is the difference between read () and recv ()?

From Andrew Gear ( andrew@erlenstar.demon.co.uk ):

read () is equivalent to recv () with the flags parameter 0. Other values ​​for the flags parameter change the behavior of recv ().
Similarly, write () is equivalent to send () with flags == 0.

+1
source
 MSG_WAITALL Block until at least len are received. However, if a signal is caught or the remote host disconnects, the function may return less data. MSG_DONTWAIT With this flag set, the function returns even if it would normally have blocked. 

The blocking ability that allows the function to wait until data is received, of course, using socket_recv, but using socket_read, it is assumed that the bytes are already received and are not waiting, so it cannot return anything:

 Note: socket_read() returns a zero length string ("") when there is no more data to read. 
0
source

Source: https://habr.com/ru/post/1415105/


All Articles