The differences between socket_read and socket_recv in PHP are described in detail. socketreadrecv. The differences between socket_read and socket_recv in PHP are described in detail. etreadrecv writes a socket network service using PHP a few days ago. the socket_read and socket_recv methods in this document are a bit dizzy. the differences between socket_read and socket_recv, socketreadrecv
I used PHP to write a socket network service a few days ago. I found that the socket_read and socket_recv methods are a bit dizzy. at first glance, this is not the same, why do we need to give two different usages. I didn't see the document clearly. I checked the source code to find out clearly. I will record it here.
Let's take a look at the declaration of these two functions:
The code is as follows:
String socket_read (resource $ socket, int $ length [, int $ type = PHP_BINARY_READ])
Int socket_recv (resource $ socket, string & $ buf, int $ len, int $ flags)
We can see from the declaration that one is to return the received data through the execution result, and the other is to return the received data through reference. Another difference is that socket_read has a type while socket_recv has a flags (chaotic enough ). Let's take a look at the source code of socket_recv!
The code is as follows:
PHP_FUNCTION (socket_recv)
{
Zval * php_sock_res, * buf;
Char * recv_buf;
Php_socket * php_sock;
Int retval;
Long len, flags;
If (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "rzll", & php_sock_res, & buf, & len, & flags) = FAILURE ){
Return;
}
ZEND_FETCH_RESOURCE (php_sock, php_socket *, & php_sock_res,-1, le_socket_name, le_socket );
/* Overflow check */
If (len + 1) <2 ){
RETURN_FALSE;
}
Recv_buf = emalloc (len + 1 );
Memset (recv_buf, 0, len + 1 );
If (retval = recv (php_sock-> bsd_socket, recv_buf, len, flags) <1 ){
Efree (recv_buf );
Zval_dtor (buf );
Z_TYPE_P (buf) = IS_NULL;
} Else {
Recv_buf [retval] = '\ 0 ';
/* Rebuild buffer zval */
Zval_dtor (buf );
Z_STRVAL_P (buf) = recv_buf;
Z_STRLEN_P (buf) = retval;
Z_TYPE_P (buf) = IS_STRING;
}
If (retval =-1 ){
PHP_SOCKET_ERROR (php_sock, "unable to read from socket", errno );
RETURN_FALSE;
}
RETURN_LONG (retval );
}
In fact, there is one line of the most critical:
The code is as follows:
If (retval = recv (php_sock-> bsd_socket, recv_buf, len, flags) <1 ){
As you can see, this function actually calls the recv of the system. it only processes the input parameters and the obtained results, which is easy to understand. Let's take a look at socket_read and socket_read, which has a $ type parameter more than the system's recv function. this is also the meaning of the function I think exists. as you can see from the document, type has two values: PHP_BINARY_READ and PHP_NORMAL_READ, which are written in the document. PHP_BINARY_READ indicates that the system recv method is used directly. PHP_NORMAL_READ indicates that the system will read the data until \ n or \ r is encountered, let's take a look at the source code:
The code is as follows:
// Omit many
If (type = PHP_NORMAL_READ ){
Retval = php_read (php_sock, tmpbuf, length, 0 );
} Else {
Retval = recv (php_sock-> bsd_socket, tmpbuf, length, 0 );
}
As you can see, if it is in PHP_NORMAL_READ mode, the behavior is the same as that of socket_recv. the recv function of the system is used. However, if it is PHP_NORMAL_READ, there is a big difference, I used my own php_read function. what about the php_read function? Let's continue to look at the source code:
The code is as follows:
* T = '\ 0 ';
While (* t! = '\ N' & * t! = '\ R' & n <maxlen ){
If (m> 0 ){
T ++;
N ++;
} Else if (m = 0 ){
No_read ++;
If (nonblock & no_read> = 2 ){
Return n;
/* The first pass, m always is 0, so no_read becomes 1
* In the first pass. no_read becomes 2 in the second pass,
* And if this is nonblocking, we shoshould return ..*/
}
If (no_read> 200 ){
Set_errno (ECONNRESET );
Return-1;
}
}
If (n <maxlen ){
M = recv (sock-> bsd_socket, (void *) t, 1, flags );
}
If (errno! = 0 & errno! = ESPIPE & errno! = EAGAIN ){
Return-1;
}
Set_errno (0 );
}
Or copy the key part. we can see that the implementation here is to call recv cyclically until \ r or \ n or the data read reaches the specified maxlen length.
Although these two functions are confusing, you should understand them here! Okay, go to bed!
Listen a few days ago, I used PHP to write a socket network service. in this document, I found that socket_read and socket_recv methods are a bit dizzy ,...