PHP 套接字与流 [英] Php Sockets vs Streams
问题描述
我认为 php 套接字和 php 流相互重叠.
我已经设法使用套接字或流制作了一个 CLI PHP 聊天客户端和一个服务器.
I think php sockets and php streams are overlapping each other.
I've managed to make a CLI PHP chat client and a server, using either sockets or streams.
这里有一些说明代码行:
使用套接字:
Here some illustrating code lines:
Using sockets:
...
$main_socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Cannot create socket.\n");
@socket_bind($main_socket, $host, $port) or die("Could not bind to socket $host : $port.\n");
@socket_listen($main_socket, 5) or die("Could not set up socket listener\n");
...
使用流:
...
$main_socket = @stream_socket_server ("tcp://$host:$port", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN) or die("Cannot create socket.\n");
$clients = array($main_socket);
$clients_peername = array(array('port' => $port));
fputs(STDOUT, "Waiting for connections...\n");
...
这里的重点是可以制作一个客户端和一个服务器使用套接字函数或流函数.
The point here is that a client and a server could be made using either sockets functions, either streams functions.
我知道 Streams 是 PHP 核心的一部分,而 Sockets 是一个扩展.
I know that Streams is part of PHP core and Sockets is an extension.
我的问题是:
- 在提到套接字编程时,套接字和流之间有什么区别?
- 是否有任何与套接字编程相关的功能,一个可以拥有而另一个则不能?
推荐答案
根据手册,sockets 扩展更底层.例如,在创建套接字时,您有更细粒度的控制,可以选择 SOCK_STREAM、SOCK_DGRAM、SOCK_SEQPACKET 等.
According to the manual, the sockets extension is more low-level. For instance, whith sockets you have finer-grained control when creating one, and can choose SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET, etc.
套接字扩展基于流行的 BSD 套接字实现了套接字通信功能的低级接口,提供了充当套接字服务器和客户端的可能性.
The socket extension implements a low-level interface to the socket communication functions based on the popular BSD sockets, providing the possibility to act as a socket server as well as a client.
有关更通用的客户端套接字接口,请参阅 stream_socket_client()、stream_socket_server()、fsockopen() 和 pfsockopen().
For a more generic client-side socket interface, see stream_socket_client(), stream_socket_server(), fsockopen(), and pfsockopen().
来源:http://www.php.net/manual/en/intro.sockets.php
这篇关于PHP 套接字与流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!