空想是没有用的,个人的能力来源于每一天的努力,而不是一步登天,不要畏惧任何新的知识,水滴石穿,总有一天会柳暗花明。
因为在以后的学习中,我可能会用到网络方面的内容,但同时很多写 PHP 的 Coder 都没写过 Socket 程序,但是肯定听说过它,也肯定听说过 网络编程 这个词,所以为了今后的学习,我打算在这里先简单的讲解下相关知识,本篇博文自带实例程序,代码托管在 码云(php-socket-base-code),你只需要下载下来,配置好相关环境,按照说明即可运行,如果无法运行,请联系我。
Socket 编程需要开启 PHP 的 Socket 扩展,我用的电脑是 Windows,所以这里你只需要打开 php.ini
文件,找到这一行去掉注释就可以了:
extension=sockets
PHP 的 Socket 编程的官方地址为:PHP Sockets
Socket 编程遵循一定的编程步骤,这几个步骤缺一不可,客户端和服务端编程有所区别,我们首先来看一下服务端。
套接字属于系统资源,我们首先调用 socket_create
方法(参考官方文档:https://www.php.net/manual/en/function.socket-create.php),调用如下:
$this->socket_handle = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$this->socket_handle) {
//创建失败抛出异常,socket_last_error 获取最后一次 socket 操作错误码,socket_strerror 打印出对应错误码所对应的可读性描述
throw new Exception(socket_strerror(socket_last_error($this->socket_handle)));
} else {
echo "create socket successful\n";
}
第一个参数指定了,当前套接字是采用 IPV4 还是 IPV6,如果是前者的话,那么传递 AF_INET,否则 AF_INET6,当然还有一种类型,就是 AF_UNIX,这个暂时不讨论,我们一般选择 AF_INET(IPV6 不是很普及)。
第二个参数,指定了协议的类型,一般选择 TCP 或者是 UDP,TCP 是可靠的流传输(生活当中用的最为广泛,保证了可靠性和安全性),UDP 则不是,这个参数一般选择 TCP。
第三个如果你之前选择了 TCP,那么它就是 SOL_TCP,否则就是 SOL_UDP。
因为一台主机可能存在多个 IP 地址,所以你需要指定你的 Socket 监听的是哪一个,常用的值为 127.0.0.1,或者是监听所有地址 0.0.0.0,那么这里可能有人不明白了,127.0.0.1 和 0.0.0.0 有啥区别呢?127.0.0.1 只是一个回环地址,只能用于本机访问,说白了就是自己玩自己的,因为这个 IP 不对外部开放,所以别人也就无法访问这个地址,所以如果你的服务器地址设置为 127.0.0.1,别人想要访问,只能去屎吧。
0.0.0.0 严格来说不算是一个 IP 地址,它的意思是本机的所有 IP 地址,都是我的,哈哈。
明白了上面这个,我们来看这个调用的代码
if (!socket_bind($this->socket_handle, $this->addr, $this->port)) {
throw new Exception(socket_strerror(socket_last_error($this->socket_handle)));
} else {
echo "bind addr successful\n";
}
是不是很简单,第一个参数就是 socket_create 返回的结果,第二个参数就是地址了,上面已经说过了,第三个参数是端口号。
经过上面的这些步骤,我们只是创建了一个套接字并且给它绑定了端口号和地址,但是系统怎么知道它是监听套接字呢?所以呢,我们的事情还没有做完,所以我们得告诉它啊,别告诉我你和系统心有灵犀啊!!!
if (!socket_listen($this->socket_handle, $this->back_log)) {
throw new Exception(socket_strerror(socket_last_error($this->socket_handle)));
} else {
echo "socket listen successful\n";
}
第二个参数值得说明一哈,请听我细细道来,对于 Linux 系统中的每一个进程而言,系统都维护着待处理套接字的队列(先进先出,总得讲个先来后到吧),上层程序处理业务逻辑总得需要时间吧,所以让你你等着你就等着呗。那么这个队列的大小设置为多大呢?它的值就是这第二个参数,那么我是不是可以设置的很大呢?骚年,你想多了吧?不同的系统这个值有所不同,别说我忽悠你,看下面。
The maximum number passed to the backlog parameter highly depends on the underlying platform. On Linux, it is silently truncated to SOMAXCONN. On win32, if passed SOMAXCONN, the underlying service provider responsible for the socket will set the backlog to a maximum reasonable value. There is no standard provision to find out the actual backlog value on this platform.
你也不必关心这个值精确的数据,没有什么意义。
经过上面的一通操作之后,我们可以开始接受来自客户端的连接了,这个函数就更简单了
$client_socket_handle = socket_accept($this->socket_handle);
这个函数的返回值也是一个 套接字句柄,所以你可以对它进行读写操作,在当前的实例程序中,我们做的事情很简单,简单到你可以怀疑人生了。
$client_socket_handle = socket_accept($this->socket_handle);
if (!$client_socket_handle) {
echo "socket_accept call failed\n";
exit(1);
} else {
while (true) {
$bytes_num = socket_recv($client_socket_handle, $buffer, 100, 0);
if (!$bytes_num) {
echo "socket_recv failed\n";
exit(1);
} else {
echo "content from client:" . $buffer . "\n";
}
}
}
以上面的例子为例,我们使用 socket_recv
读取来自客户端的内容,这个函数很简单,函数原型如下:
socket_recv ( resource $socket , string &$buf , int $len , int $flags ) : int
读取的内容会在第二个参数返回,第二个参数传递我们想要读取的字符数,第四个参数可以直接设置为 0,该函数的返回值为实际读取的字节数。
客户端相对于服务端来说,就很简单了,流程如下:
创建套接字前面已经讲过了,不再详述,客户端只需要连接服务器即可,函数为 socket_create,我们来看一哈在当前的例子中,我们是如何调用的。
if (!socket_connect($this->socket_handle, $this->server_addr, $this->server_port)) {
echo socket_strerror(socket_last_error($this->socket_handle)) . "\n";
exit(1);
} else {
while (true) {
$data = fgets(STDIN);
//如果用户输入 quit,那么退出程序
if (strcmp($data, "quit") == 0) {
break;
}
socket_write($this->socket_handle, $data);
}
}
该函数只需要指定服务器的地址和端口号即可,参数是不是很简单
在讲解基本函数调用的时候,我就把自带程序的核心部分,复制出来了,如果要完整的程序,这里是地址(php-socket-base-code),代码非常简单,再次提醒,这些代码完全是用于给大家讲解基本的 socket 编程操作,为大家以后的学习打下基础,那么如何使用这个例子程序呢?
进入到命令行,开启服务器程序
php TcpServer.php
打开另外一个命令行界面,
php TcpClient.php
在客户端界面,输入任何文本,再输入回车,再切换到服务器界面,您将会看到客户端输入的内容。
在笔者的电脑上操作实例截图如下:
php-socket-base-code / TcpServer.php
<?php
class TcpServer
{
private $port = 8080;
private $addr = "127.0.0.1";
private $socket_handle;
private $back_log = 10;
private $websocket_key;
private $current_message_length;
private $is_shakehanded = false;
private $mask_key;
public function __construct($port = 8080, $addr = "127.0.0.1", $back_log = 10)
{
$this->port = $port;
$this->addr = $addr;
$this->back_log = $back_log;
}
/**
* @throws Exception
*/
private function createSocket()
{
//创建 socket 套接字
$this->socket_handle = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$this->socket_handle) {
//创建失败抛出异常,socket_last_error 获取最后一次 socket 操作错误码,socket_strerror 打印出对应错误码所对应的可读性描述
throw new Exception(socket_strerror(socket_last_error($this->socket_handle)));
} else {
echo "create socket successful\n";
}
}
/**
* @throws Exception
*/
private function bindAddr()
{
if (!socket_bind($this->socket_handle, $this->addr, $this->port)) {
throw new Exception(socket_strerror(socket_last_error($this->socket_handle)));
} else {
echo "bind addr successful\n";
}
}
private function listen()
{
if (!socket_listen($this->socket_handle, $this->back_log)) {
throw new Exception(socket_strerror(socket_last_error($this->socket_handle)));
} else {
echo "socket listen successful\n";
}
}
/**
* @throws Exception
*/
private function accept()
{
$client_socket_handle = socket_accept($this->socket_handle);
if (!$client_socket_handle) {
echo "socket_accept call failed\n";
exit(1);
} else {
while (true) {
$bytes_num = socket_recv($client_socket_handle, $buffer, 100, 0);
if (!$bytes_num) {
echo "socket_recv failed\n";
exit(1);
} else {
echo "content from client:" . $buffer . "\n";
}
}
}
}
public function startServer()
{
try {
$this->createSocket();
$this->bindAddr();
$this->listen();
$this->accept();
} catch (Exception $exception) {
echo $exception->getMessage() . "\n";
}
}
}
$server = new TcpServer();
$server->startServer();
php-socket-base-code / TcpClient.php
<?php
class TcpClient
{
private $server_port;
private $server_addr;
private $socket_handle;
public function __construct($port = 8080, $addr = "127.0.0.1")
{
$this->server_addr = $addr;
$this->server_port = $port;
}
/**
* @throws Exception
*/
private function createSocket()
{
//创建 socket 套接字
$this->socket_handle = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$this->socket_handle) {
//创建失败抛出异常,socket_last_error 获取最后一次 socket 操作错误码,socket_strerror 打印出对应错误码所对应的可读性描述
throw new Exception(socket_strerror(socket_last_error($this->socket_handle)));
} else {
echo "create socket successful\n";
}
}
public function connectToServer()
{
$this->createSocket();
if (!socket_connect($this->socket_handle, $this->server_addr, $this->server_port)) {
echo socket_strerror(socket_last_error($this->socket_handle)) . "\n";
exit(1);
} else {
while (true) {
$data = fgets(STDIN);
//如果用户输入 quit,那么退出程序
if (strcmp($data, "quit\n") == 0) {
break;
}
socket_write($this->socket_handle, $data);
}
}
}
}
$client = new TcpClient();
$client->connectToServer();
本文为转载,原文地址: