_php examples of socket_read and socket_recv differences in PHP

Source: Internet
Author: User

A few days ago, using PHP to write a socket network service, in the document to see Socket_read and Socket_recv these two methods when a little dizzy, at first glance this is not the same, why also give two different usage. Look at the document did not see too clear, looked at the source code to get clear, here to record.

Let's take a look at the declarations of these two functions:

Copy Code code 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)

As you can see from the declaration, one is to return the received data through the execution result, and the other is to return the received data in the form of a reference. Another difference is that socket_read a type,socket_recv more than a flags (messy enough). Let's take a look at Socket_recv's source code first!
Copy Code code 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] = ' the ';

/* 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);
}

There's a whole bunch of them, but there's one line that matters:

Copy Code code as follows:

if ((retval = recv (Php_sock->bsd_socket, RECV_BUF, Len, Flags)) < 1) {

You can see, in fact, this function is called the recv of the system, but the input parameters and obtained results are processed, better understanding. So let's take a look at it. Socket_read,socket_read has a $type parameter than the RECV function of the system, which I think is the meaning of this function, as you can see from the document that type has two values, respectively, Php_binary_read and Php_ Normal_read, there are written in the document, Php_binary_read represents the Recv method directly using the system, Php_normal_read said will read, until encountered \ n or \ r, we look at the source code:
Copy Code code as follows:

Omit a large pile of
if (type = = Php_normal_read) {
retval = Php_read (Php_sock, tmpbuf, length, 0);
} else {
retval = recv (Php_sock->bsd_socket, tmpbuf, length, 0);
}

Can be seen, if it is php_normal_read mode, in fact, the behavior and SOCKET_RECV is the same, are used in the system of the RECV function, but if it is php_normal_read, there is a big difference, with its own implementation of the Php_read function, So what does this php_read do? We continue to look at the source code:
Copy Code code as follows:

*t = ' n ';
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 "I", M always is 0, so no_read becomes 1
* In the ' the '. No_read becomes 2 in the second pass,
* And if this is nonblocking, we should 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 is copy the key part, you can see that the implementation here is to loop the call recv until you encounter \ R or \ n or read the length of the data to the specified maxlen.

Although these two functions are more chaotic, but see here should understand it! All right, go to bed!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.