anru
anru

Reputation: 1414

PHP socket_recv and socket_read

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?

Upvotes: 7

Views: 12063

Answers (3)

Ahmed Jolani
Ahmed Jolani

Reputation: 3072

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:

Note:

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

Upvotes: 1

Esailija
Esailija

Reputation: 140220

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.

Upvotes: 2

bart s
bart s

Reputation: 5100

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

Upvotes: 16

Related Questions