Home >Backend Development >PHP Tutorial >Let's talk about PHP SOCKET programming (with detailed explanation)
在PHP中有太多我们需要学习和了解的东西,今天这篇文章就让我们一起聊聊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 交互式处理等
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);
客户端代码:
<?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);
使用cli方式启动server:
<span style="font-family:Arial,Helvetica,Georgia,sans-serif; color:#525454"><span style="line-height:24px">php server.php</span></span><span style="font-family:Arial,Helvetica,Georgia,sans-serif; color:#525454"><span style="line-height:24px"></span></span><span style="font-family:Arial,Helvetica,Georgia,sans-serif; color:#525454"><span style="line-height:24px"></span></span>
这里注意socket_read函数:
The optional type parameter is a named constant:
PHP_BINARY_READ - uses the system recv() function. Security for reading binary data. (Default in PHP > = 4.1.0)
PHP_NORMAL_READ - Read stops at \n or \r (Default in PHP <= 4.0.6)
##For the parameter PHP_NORMAL_READ, if the server's response result is not \ n. Cause 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 solved the problem of concurrency
IO through multi-process and multi-thread. 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 thread model is
The biggest problem with this model is that the creation and destruction of threads in the process / is very expensive. big. 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.
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), and the multi-process model is unable to meet its needs.
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 quite 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 concurrencyIO in one process? The answer is yes, this is IO multiplexing technology.
##In factIOThe history of reuse is as long as multi-process, Linux has been provided for a long timeselect system 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: ##Reactor is just an event generator, actually for socket handle operations, such as connect/accept, send/recv、close is in callback Completed in . For specific coding, please refer to the following pseudo code: ##ReactorThe 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 ##
4. PHP socket内部源码 从PHP内部源码来看,PHP提供的socket编程是在socket,bind,listen等函数外添加了一个层,让其更加简单和方便调用。但是一些业务逻辑的程序还是需要程序员自己去实现。 Zend API实际对c函数socket做了包装,供PHP使用。 而在c的socket编程中,我们使用如下方式初始化socket。
5. socket函数 Function name description
6. PHP Socket模拟请求 我们使用stream_socket来模拟: 注意,以上程序可能会进入死循环; 这个PHP的feof($fp) 需要注意的地方了,我们来分析为什么进入死循环。 实际上,feof是可靠的,但是结合fgets函数一块使用的时候,必须要小心了。一个常见的做法是: 当处理纯文本的时候,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_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;
ZEND_REGISTER_RESOURCE(return_value, php_sock, le_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);
}
socket_accept() Accept a Socket connection
socket_bind() Bind the socket to an IP On the address and port
socket_clear_error() clears the socket error or the last error code
socket_close() closes a socket resource
socket_connect() Starts a socket connection
socket_create_listen() Opens a socket to listen on the specified port
socket_create_pair() Generates a pair of undifferentiated sockets into an array
socket_create() generates a socket, which is equivalent to generating a socket data structure
socket_get_option() Gets the socket option
socket_getpeername() Get the IP address of a remote similar host
socket_getsockname() Get the IP address of the local socket
socket_iovec_add() Adds a new vector to a scatter/aggregate array
socket_iovec_alloc() This function creates an iovec data structure that can send, receive, read and write
socket_iovec_delete() deletes an allocated iovec
socket_iovec_fetch() returns the data of the specified iovec resource
socket_iovec_free() releases an iovec resource
socket_iovec_set() Set the new value of iovec data
socket_last_error() Get the last error code of the current socket
socket_listen() Listen by All connections of the specified socket
socket_read() reads data of the specified length
socket_readv() reads data from the scatter/aggregate array
socket_recv() Ends data from the socket to the cache
socket_recvfrom() Accepts data from the specified socket. If not specified, it defaults to the current socket
socket_recvmsg() Receive messages from iovec
socket_select() Multiple selection
socket_send() This function sends data to the connected socket
socket_sendmsg() Send a message to the socket
socket_sendto() Send a message to the socket at the specified address
socket_set_block() in the socket Set to block mode
socket_set_nonblock() Set to non-block mode in socket
socket_set_option() Set socket option
socket_shutdown() This function allows you to close reading, writing, or the specified socket
socket_strerror() Returns a detailed error with the specified error number
socket_write() Writes data to the socket cache
socket_writev() writes data to the scatter/aggregate array/**
*
* @param $data= array=array('key'=>value)
*/
function post_contents($data = array()) {
$post = $data ? http_build_query($data) : '';
$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 = '';
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;
}
while ( !feof($fp) ) {
$r = fgets($fp, 1024);
$response .= $r;
}
$fp = fopen("myfile.txt", "r");
while (!feof($fp)) {
$current_line = fgets($fp);
//对结果做进一步处理,防止进入死循环
}
The above is the detailed content of Let's talk about PHP SOCKET programming (with detailed explanation). For more information, please follow other related articles on the PHP Chinese website!