这篇文章主要介绍了PHP中的socket_read和socket_recv区别详解,本文从源码上分析了这两个函数的不同之处,需要的朋友可以参考下
前几天用PHP写一个socket网络服务,在文档里看到socket_read和socket_recv这两个方法时有点晕,乍一看这不是一样的嘛,干吗还要给两个不同的用法呢。看文档没看太明白,看了下源码才搞清楚,在这里记录一下。
先看一下这两个函数的声明:
复制代码 代码如下:
string socket_read ( resource $socket , int $length [, int $type = PHP_BINARY_READ ] )
int socket_recv ( resource $socket , string &$buf , int $len , int $flags )
复制代码 代码如下:
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)
RETURN_FALSE;
}
recv_buf = emalloc(len + 1);
memset(recv_buf, 0, len + 1);
if ((retval = recv(php_sock->bsd_socket, recv_buf, len, flags)) 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);
}
啰里啰嗦一大堆,其实有一行最关键:
复制代码 代码如下:
if ((retval = recv(php_sock->bsd_socket, recv_buf, len, flags))
复制代码 代码如下:
//省略一大堆
if (type == PHP_NORMAL_READ) {
retval = php_read(php_sock, tmpbuf, length, 0);
} else {
retval = recv(php_sock->bsd_socket, tmpbuf, length, 0);
}
复制代码 代码如下:
*t = '\0';
while (*t != '\n' && *t != '\r' && n
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 should return.. */
}
if (no_read > 200) {
set_errno(ECONNRESET);
return -1;
}
}
if (n
m = recv(sock->bsd_socket, (void *) t, 1, flags);
}
if (errno != 0 && errno != ESPIPE && errno != EAGAIN) {
return -1;
}
set_errno(0);
}
虽然这两个函数比较混乱,但是看到这里应该明白了吧!好了睡觉去啦!