Home » Php » PHP socket_recv and socket_read

PHP socket_recv and socket_read

Posted by: admin July 12, 2020 Leave a comment


From reading the PHP manual, socket_recv and socket_read function looks
same to me, both function get data from client.

can any one tell me what are different between those 2 functions?

How to&Answers:

socket_recv returns the number of bytes received
socket_read returns the data that has been received

With socket_recv you can read bytes from the buffer AND know how many bytes have been recevied. With socket_read you can only read a specific amount of data from the buffer


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

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

From Andrew Gierth ([email protected]):

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


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.

A blocking ability which will let the function wait untill a data received of course by using the socket_recv, but by using socket_read it assumes that bytes already received and does not wait, so it may return nothing:


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