Home > Web Front-end > JS Tutorial > body text

Detailed explanation of SOCKET programming in PHP

小云云
Release: 2018-03-28 15:10:47
Original
5186 people have browsed it

本文主要和大家分享PHP之SOCKET编程详解,主要结合文字和代码的形式和大家分享,希望能帮助到大家。

1. 预备知识

       一直以来很少看到有多少人使用php的socket模块来做一些事情,大概大家都把它定位在脚本语言的范畴内吧,但是其实php的socket模块可以做很多事情,包括做ftplist,http post提交,smtp提交,组包并进行特殊报文的交互(如smpp协议),whois查询。这些都是比较常见的查询。

特别是php的socket扩展库可以做的事情简直不会比c差多少。
php的socket连接函数
1、集成于内核的socket
这个系列的函数仅仅只能做主动连接无法实现端口监听相关的功能。而且在4.3.0之前所有socket连接只能工作在阻塞模式下。
此系列函数包括
fsockopen,pfsockopen
这两个函数的具体信息可以查询php.net的用户手册
他们均会返回一个资源编号对于这个资源可以使用几乎所有对文件操作的函数对其进行操作如fgets(),fwrite(), fclose()等单注意的是所有函数遵循这些函数面对网络信息流时的规律,例如:
fread() 从文件指针 handle 读取最多 length 个字节。 该函数在读取完 length 个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时就会停止读取文件,视乎先碰到哪种情况。 
可以看出对于网络流就必须注意取到的是一个完整的包就停止。
2、php扩展模块带有的socket功能。
php4.x 以后有这么一个模块extension=php_sockets.dll,Linux上是一个extension=php_sockets.so。
当打开这个此模块以后就意味着php拥有了强大的socket功能,包括listen端口,阻塞及非阻塞模式的切换,multi-client 交互式处理等
这个系列的函数列表参看http://www.php.net/manual/en/ref.sockets.php
看过这个列表觉得是不是非常丰富呢?不过非常遗憾这个模块还非常年轻还有很多地方不成熟,相关的参考文档也非常少:(
我也正在研究中,因此暂时不具体讨论它,仅给大家一个参考文章。

2. 使用PHP socket扩展

服务器端代码:

<?php
/**
 * File name server.php
 * 服务器端代码
 * 
 * @author guisu.huang
 * @since 2012-04-11
 * 
 */

//确保在连接客户端时不会超时
set_time_limit(0);
//设置IP和端口号
$address = "127.0.0.1";
$port = 2046; //调试的时候,可以多换端口来测试程序!
/**
 * 创建一个SOCKET 
 * AF_INET=是ipv4 如果用ipv6,则参数为 AF_INET6
 * SOCK_STREAM为socket的tcp类型,如果是UDP则使用SOCK_DGRAM
*/
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");
//阻塞模式
socket_set_block($sock) or die("socket_set_block() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");
//绑定到socket端口
$result = socket_bind($sock, $address, $port) or die("socket_bind() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");
//开始监听
$result = socket_listen($sock, 4) or die("socket_listen() 失败的原因是:" . socket_strerror(socket_last_error()) . "/n");
echo "OK\nBinding the socket on $address:$port ... ";
echo "OK\nNow ready to accept connections.\nListening on the socket ... \n";
do { // never stop the daemon
	//它接收连接请求并调用一个子连接Socket来处理客户端和服务器间的信息
	$msgsock = socket_accept($sock) or  die("socket_accept() failed: reason: " . socket_strerror(socket_last_error()) . "/n");
	
	//读取客户端数据
	echo "Read client data \n";
	//socket_read函数会一直读取客户端数据,直到遇见\n,\t或者\0字符.PHP脚本把这写字符看做是输入的结束符.
	$buf = socket_read($msgsock, 8192);
	echo "Received msg: $buf   \n";
	
	//数据传送 向客户端写入返回结果
	$msg = "welcome \n";
	socket_write($msgsock, $msg, strlen($msg)) or die("socket_write() failed: reason: " . socket_strerror(socket_last_error()) ."/n");
	//一旦输出被返回到客户端,父/子socket都应通过socket_close($msgsock)函数来终止
    socket_close($msgsock);
} while (true);
socket_close($sock);
Copy after login

客户端代码:

<?php
/**
 * File name:client.php
 * 客户端代码
 * 
 * @author guisu.huang
 * @since 2012-04-11
 */
set_time_limit(0);

$host = "127.0.0.1";
$port = 2046;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)or die("Could not create	socket\n"); // 创建一个Socket
 
$connection = socket_connect($socket, $host, $port) or die("Could not connet server\n");    //  连接
socket_write($socket, "hello socket") or die("Write failed\n"); // 数据传送 向服务器发送消息
while ($buff = socket_read($socket, 1024, PHP_NORMAL_READ)) {
    echo("Response was:" . $buff . "\n");
}
socket_close($socket);
Copy after login

使用cli方式启动server:

php server.php


这里注意socket_read函数:

可选的类型参数是一个命名的常数:
PHP_BINARY_READ - 使用系统recv()函数。用于读取二进制数据的安全。 (在PHP>“默认= 4.1.0)
PHP_NORMAL_READ - 读停在\ n或\r(在PHP <= 4.0.6默认)

For the parameter PHP_NORMAL_READ, if the server's response result is not \ n. Causes socket_read(): unable to read from socket


#3. Concurrent IO programming in PHP

Original text: http://rango .swoole.com/archives/508


1) Multi-process/multi-thread synchronization blocking

##The earliest server-side programs are all through multi-process and multi-thread To solve the problem of concurrency

IO. The process model appeared earliest, and the concept of process has been around since the birth of the Unix system. The earliest server-side programs are generallyAcceptA process is created when a client connects, and then the child process enters the loop synchronization blocking area. Interact with client connections and send and receive processing data.


Multi-threading mode appeared later. Threads are lighter than processes, and the memory stack is shared between threads. , so interaction between different threads is very easy to implement. For example, in a program like a chat room, client connections can interact with each other, and players in the chat room can send messages to any other person. It is very simple to implement in multi-thread mode, and a client connection can be directly read and written in the thread. The multi-process mode requires the use of pipelines, message queues, and shared memory to achieve data interaction, collectively referred to as inter-process communication (

IPC) and complex technologies can be achieved.

Code example:

Multiple processes/The process of the thread model is

  1. Create a socket, bind the server port (bind), and listen on the port (listen), use in PHP stream_socket_serverOne function can complete the above 3 steps, of course you can also use php sockets extensions are implemented separately.

  2. Enters the while loop, blocking at acceptIn operation, wait for the client connection to come in. At this time, the program will enter a sleep state until a new client initiates a connect to the server, and the operating system will wake up the process. acceptThe function returns the socket

  3. The main process passes fork(#php: pcntl_fork) to create a child process, use pthread_create(php under the multi-threading model : new Thread) creates a child thread. Unless otherwise stated below, process will also be used to represent process / thread.

  4. After the child process is successfully created, it enters the while loop, blocking at recvphp: fread) call, waiting for the client to send data to the server. After receiving the data, the server program processes it and then uses send(php: fwrite) sends a response to the client. A long-connection service will continue to interact with the client, while a short-connection service will generally close after receiving a response.

  5. #When the client connection is closed, the child process exits and destroys all resources. The main process will recycle this child process.

The biggest problem with this model is that the process /Thread creation and destruction are expensive. So the above model cannot be applied to very busy server programs. The corresponding improved version solves this problem. This is the classic Leader-Follower model.

Code example:


Its characteristic is that it will be created after the program startsN processes. Each child process enters Accept and waits for new connections to come in. When the client connects to the server, one of the child processes will be awakened, start processing the client request, and no longer accept new TCP connections. When this connection is closed, the child process will be released and re-enter Accept to participate in processing new connections.

The advantage of this model is that it can completely reuse the process, without additional consumption, and the performance is very good. Many common server programs are based on this model, such as Apache, PHP-FPM.

The multi-process model also has some disadvantages.

  1. This model relies heavily on the number of processes to solve concurrency problems. A client connection needs to occupy one process, how many worker processes are there, and concurrent processing capabilities. There are as many as there are. The operating system is limited in the number of processes it can create.

  2. #Starting a large number of processes will bring additional process scheduling consumption. When there are hundreds of processes, the process context switching scheduling consumption may account for less than CPU1% You can ignore it. If you start thousands or even tens of thousands of processes, the consumption will skyrocket. Scheduling consumption may account for tens of percent of CPU or even 100%.

#There are also some scenarios that the multi-process model cannot solve, such as instant chat programs (IM), a server must maintain tens of thousands or even hundreds of thousands or millions of connections at the same time (the classic C10K problem), the multi-process model is It’s beyond my capabilities.

There is another scenario that is also the weakness of the multi-process model. Typically Web the server starts 100 processes if one request consumes 100ms, 100 processes can provide 1000qps, this processing capability is pretty good. But if the request requires calling the external network Http interface, like QQ, Weibo login will take a long time, one request takes 10s. That process can only handle 0.1 requests in 1 seconds, 100 processes can only reach 10qps. This processing capability is too poor. .

Is there a technology that can handle all concurrent IO in one process? The answer is yes, this is IO multiplexing technology.

IOReuse/Event loop/Asynchronous non-blocking

In factIOThe history of reuse is as long as multi-process, Linux has been provided for a long timeselectSystem call can maintain 1024 connections within a process. Later, the poll system call was added, and poll made some improvements. Solved the problem of 1024 limit and can maintain any number of connections. But select/poll Another problem is that it needs to loop to detect whether there are events on the connection. The problem arises. If the server has 100 million connections, and only one connection sends data to the server at a certain time, select/pollNeeds to loop 100 million times, of which only 1 times are hits, and the remaining 9910,0009999 times are invalid, wasting CPU resources.

UntilLinux 2.6 the kernel provides the new epollSystem call can maintain an unlimited number of connections without polling, which truly solves the C10K problem. Nowadays, various high-concurrency asynchronous IO server programs are based on epoll Implemented, such as Nginx, Node.js, Erlang, Golang. A single-process, single-threaded program like Node.js can last for more than 1 Millions##TCP connections, all thanks to epoll technology.

##IOReuse asynchronous non-blocking programs using the classic Reactor Model, Reactor As the name suggests, it means reactor. It does not process any data sending and receiving itself. You can only monitor the event changes of a socket handle.

##Reactor

4 core operations:

  1. addAddsocketListen to reactor, can be listen socket can also make the clientsocket, or it can be a pipe, eventfd , signals, etc.

  2. set#Modify event monitoring, you can set the monitoring Type, such as readable, writable. Readable and easy to understand, for listen socket means that a new client connection has arrived and requires accept. For client connections to receive data, recv is required. Writable events are a bit more difficult to understand. A SOCKET has a cache area. If you want to connect to the client, send 2M data cannot be sent out at one time. The operating system default TCP cache area only has 256K. Only 256K can be sent at one time. Once the buffer is full, send will Will return EAGAIN error. At this time, you need to monitor writable events. In pure asynchronous programming, you must monitor writable events to ensure that the send operation is completely non-blocking.

  3. ##delfromreactorRemove from, no longer listen to events

  4. callback is the corresponding processing logic after the event occurs, usually in add/set## Formulated when #. CThe language is implemented with function pointers, JSYou can use anonymous functions, PHPYou can use anonymous functions, object method arrays, and string function names.


##Reactor is just an event generator, actually for socket Handle operations, such as connect/accept, send/recv#、close is at callback Completed in . For specific coding, please refer to the following pseudo code:


##Reactor

The model can also be used with multi-process, The combination of multiple threads not only achieves asynchronous non-blocking IO, but also takes advantage of multiple cores. The current popular asynchronous server programs are all in this way: such as

  • Nginx

    : multi-process Reactor

  • Nginx+Lua

    : Multi-processReactor+Coroutine

  • ##Golang
  • :Single ThreadReactor+Multi-threaded coroutine

  • Swoole:多线程Reactor+多进程Worker


4. PHP socket内部源码

从PHP内部源码来看,PHP提供的socket编程是在socket,bind,listen等函数外添加了一个层,让其更加简单和方便调用。但是一些业务逻辑的程序还是需要程序员自己去实现。
下面我们以socket_create的源码实现来说明PHP的内部实现。
前面我们有说到php的socket是以扩展的方式实现的。在源码的ext目录,我们找到sockets目录。这个目录存放了PHP对于socket的实现。直接搜索PHP_FUNCTION(socket_create),在sockets.c文件中找到了此函数的实现。如下所示代码:

/* {{{ proto resource socket_create(int domain, int type, int protocol) U
   Creates an endpoint for communication in the domain specified by domain, of type specified by type */
PHP_FUNCTION(socket_create)
{
        long            arg1, arg2, arg3;
        php_socket      *php_sock = (php_socket*)emalloc(sizeof(php_socket));
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &arg1, &arg2, &arg3) == FAILURE) {
                efree(php_sock);
                return;
        }
 
        if (arg1 != AF_UNIX
#if HAVE_IPV6
                && arg1 != AF_INET6
#endif
                && arg1 != AF_INET) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket domain [%ld] specified for argument 1, assuming AF_INET", arg1);
                arg1 = AF_INET;
        }
 
        if (arg2 > 10) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket type [%ld] specified for argument 2, assuming SOCK_STREAM", arg2);
                arg2 = SOCK_STREAM;
        }
 
        php_sock->bsd_socket = socket(arg1, arg2, arg3);
        php_sock->type = arg1;
 
        if (IS_INVALID_SOCKET(php_sock)) {
                SOCKETS_G(last_error) = errno;
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create socket [%d]: %s", errno, php_strerror(errno TSRMLS_CC));
                efree(php_sock);
                RETURN_FALSE;
        }
 
        php_sock->error = 0;
        php_sock->blocking = 1;
                                                                                                                                           1257,1-8      61%
        ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
}
/* }}} */
Copy after login

Zend API实际对c函数socket做了包装,供PHP使用。 而在c的socket编程中,我们使用如下方式初始化socket。

//初始化Socket  
    if( (socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){  
         printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);  
         exit(0);  
    }
Copy after login

5. socket函数

Function name Description
socket_accept() Accepts a Socket connection
socket_bind() Binds the socket to an IP address and port
socket_clear_error () Clear the socket error or last error code
socket_close() Close a socket resource
socket_connect() Start a socket connection
socket_create_listen() Open a socket listening on the specified port
socket_create_pair() Generate a pair of undifferentiated sockets into an array
socket_create() Generate a socket , equivalent to generating a socket data structure
socket_get_option() Gets the socket option
socket_getpeername() Gets the ip address of a remote similar host
socket_getsockname() Get the ip address of the local socket
socket_iovec_add() Add a new vector to a scatter/aggregate array
socket_iovec_alloc() This function creates An iovec data structure capable of sending, receiving, reading and writing
socket_iovec_delete() Delete an allocated iovec
socket_iovec_fetch() Return the data of the specified iovec resource
socket_iovec_free() Releases an iovec resource
socket_iovec_set() Sets the new value of iovec data
socket_last_error() Gets the last error code of the current socket
socket_listen() listens to all connections from the specified socket
socket_read() 读取指定长度的数据
socket_readv() 读取从分散/聚合数组过来的数据
socket_recv() 从socket里结束数据到缓存
socket_recvfrom() 接受数据从指定的socket,如果没有指定则默认当前socket
socket_recvmsg() 从iovec里接受消息
socket_select() 多路选择
socket_send() 这个函数发送数据到已连接的socket

socket_sendmsg() 发送消息到socket
socket_sendto() 发送消息到指定地址的socket
socket_set_block() 在socket里设置为块模式
socket_set_nonblock() socket里设置为非块模式
socket_set_option() 设置socket选项
socket_shutdown() 这个函数允许你关闭读、写、或指定的socket
socket_strerror() 返回指定错误号的周详错误
socket_write() 写数据到socket缓存
socket_writev() 写数据到分散/聚合数组

6. PHP Socket模拟请求

我们使用stream_socket来模拟:

/**
 * 
 * @param $data= array=array(&#39;key&#39;=>value)
 */
function post_contents($data = array()) {
    $post = $data ? http_build_query($data) : &#39;&#39;;
    $header = "POST /test/ HTTP/1.1" . "\n";
    $header .= "User-Agent: Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1)" . "\n";
    $header .= "Host: localhost" . "\n";
    $header .= "Accept: */*" . "\n";
    $header .= "Referer: http://localhost/test/" . "\n";
    $header .= "Content-Length: ". strlen($post) . "\n";
    $header .= "Content-Type: application/x-www-form-urlencoded" . "\n";
    $header .= "\r\n";
    $ddd = $header . $post;
    $fp = stream_socket_client("tcp://localhost:80", $errno, $errstr, 30);
    $response = &#39;&#39;;
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        fwrite($fp, $ddd);
        $i = 1;
        while ( !feof($fp) ) {
            $r = fgets($fp, 1024);
            $response .= $r;
            //处理这一行
        }
    }
    fclose($fp);
    return $response;
}
Copy after login

注意,以上程序可能会进入死循环;

这个PHP的feof($fp) 需要注意的地方了,我们来分析为什么进入死循环。

        while ( !feof($fp) ) {
            $r = fgets($fp, 1024);
            $response .= $r;
        }
Copy after login

实际上,feof是可靠的,但是结合fgets函数一块使用的时候,必须要小心了。一个常见的做法是:


$fp = fopen("myfile.txt", "r");
while (!feof($fp)) {
   $current_line = fgets($fp);
   //对结果做进一步处理,防止进入死循环
}
Copy after login

当处理纯文本的时候,fgets获取最后一行字符后,foef函数返回的结果并不是TRUE。实际的运算过程如下:

 1) while()继续循环。

 2) fgets 获取倒数第二行的字符串

 3) feof返回false,进入下一次循环

 4)fgets获取最后一行数据

 5)  一旦fegets函数被调用,feof函数仍然返回的是false。所以继续执行循环

 6) fget试图获取另外一行,但实际结果是空的。实际代码没有意识到这一点,试图处理另外根本不存在的一行,但fgets被调用了,feof放回的结果仍然是false

 7)    .....

8) 进入死循环

相关推荐:

PHP Socket编程起步

The above is the detailed content of Detailed explanation of SOCKET programming in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!