您的位置:首页 > 编程语言 > PHP开发

使用php socket创建简单服务器

2018-03-02 17:34 513 查看

创建一个http服务器步骤:

创建socket套接字

绑定某个地址和端口

开始监听,并根据客户端的请求做出响应

关闭socket(可以省略,php可以自动回收资源)

PHP代码

<?php
/**
* @description HttpServer类
* @author luoluolzb <luoluolzb@163.com>
* @date   2018/3/2
*/
class HttpServer
{
protected $port;
protected $address;
protected $socket;

/**
* 构造函数
* @param string  $address 监听地址
* @param integer $port    监听端口
*/
public function __construct($address = 'localhost', $port = 80)
{
$this->port = $port;
$this->address = $address;
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (! $this->socket) {
throw new Exception("Http Server create failed!");
}
//绑定地址和端口
socket_bind($this->socket, $address, $this->port);
}

/**
* 析构函数
*/
public function __destruct()
{
socket_close($this->socket);
}

/**
* 开始运行http服务器
*/
public function run()
{
//开始进行监听
socket_listen($this->socket);
while (true) {
//获取请求socket
$msg_socket = socket_accept($this->socket);
//获取请求内容
$buf = socket_read($msg_socket, 99999);
echo $buf;  //输出请求内容
//写入相应内容(输出"Hello World!")
socket_write($msg_socket, $this->text("Hello World!"));
//关闭请求socket
socket_close($msg_socket);
}
}

/**
* 获取http协议的文本内容
* @param  string $content string
* @return string
*/
private function text($content)
{
//协议头
$text = 'HTTP/1.0 200 OK' . "\r\n";
$text .= 'Content-Type: text/plain' . "\r\n";
$text .= 'Content-Length: ' . strlen($content) . "\r\n";

//以空行分隔
$text .= "\r\n";

//协议正文
$text .= $content;
return $text;
}
}

//测试
$server = new HttpServer();
echo "Server running at http://localhost\r\n"; $server->run();


运行过程

使用命令行运行脚本
php HttpServer.php


浏览器访问地址
http://localhost


查看结果:

控制台截图:



浏览器截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: