资源简介
在linux下面,自动动手用socket实现了一个简单的php web server。经过测试,可以使用。

代码片段和文件信息
/*
* =====================================================================================
*
* Filename: phpServer.cpp
* Version: 0.1
* Created: 2011年05月16日 07时40分56秒
* Compiler: g++
*
* Author: zhxm501205289@qq.com
* Company: xd-1301
*
* =====================================================================================
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//缓冲区大小
#define MAX_BUF_SIZE 1024*1024
//分段发送函数
int mySend(void* bufint sizeint sockfd)
{
int sended=0;
while(1)
{
int flag = send(sockfd (char*)buf+sendedsize-sended0);
if(flag ==-1)//发送失败
{
//重新试一试
flag = send(sockfd (char*)buf+sendedsize-sended0);
if(flag == -1)//如果还是失败,则放弃发送
break;
}
sended +=flag;
if(sended>=size)
{
break;
}
}
return sended;
}
//返回 PHP-cgi消息的头和内容的分界点
//php-cgi处理php的返回消息格式如下
//head \r\n
//\r\n
//content \r\n
char* dealPHPMes(char* dataint len)
{
char* p = data;
char* end = p+len-3;
for(;p {
if(*p==‘\r‘ && *(p+1) ==‘\n‘ && *(p+2)==‘\r‘ && *(p+3)==‘\n‘)
return p+4;
}
return data;
}
//从http请求头中获取请求的文件
//http典型请求头如下:
//GET /a.php?a=10&b=10 HTTP/1.1 \r\n
//
//函数返回./a.php
string getReq(char* bufint len)
{
string temp = “.“;
char *p=buf;
char* end = buf+len;
for(;p if(*p==‘/‘) break;
for(;p {
//空格 或者\r\n是典型的结束符号
if(*p==‘?‘ || *p == ‘ ‘) break;
temp +=*p;
}
return temp;
}
//从http请求头中获取请求的文件的类型
//http典型请求头如下:
//GET /a.php?a=10&b=10 HTTP/1.1 \r\n
//
//函数返回“php“
string getType(char* bufint len)
{
string temp = ““;
char *p=buf;
char* end = buf+len;
for(;p if(*p==‘.‘) break;
for(p++;p {
if(*p==‘?‘ || *p==‘ ‘) break;
temp +=*p;
}
return temp;
}
//从http请求头中获取请求的参数
//http典型请求头如下:
//GET /a.php?a=10&b=10 HTTP/1.1 \r\n
//
//函数返回“a=10 b=10“
string getArg(char* bufint len)
{
string temp = ““;
string method;
char tempBuf[10];
char *p=buf;
char* end = buf+len;
sscanf(buf“%s“tempBuf);
method=tempBuf;
if(method==“GET“)//只考虑GET POST 两种方法 GET方法参数紧跟请求文件后面 如a.php?a=10&b=10
{
for(;p if(*p==‘?‘) break;//‘?‘是参数列表的开始符号
for(p++;p {
if(*p==‘\r‘ && *(p+1)==‘\n‘) break;
if(*p==‘ ‘)break;
if(*p==‘&‘)//a=10&b=10 转成a=10 b=10
temp +=‘ ‘;
else
temp += *p;
}
return temp;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 37002 2011-05-05 19:24 0.png
文件 386 2011-05-17 13:02 a.php
文件 9268 2011-05-17 20:04 phpServer.cpp
----------- --------- ---------- ----- ----
46656 3
- 上一篇:采用php编写在线聊天室
- 下一篇:PHP和MySQL部门考核系统
相关资源
- 校园疫情防控进出登记管理系统THIN
- 手写签名,PHP +js+html5
- 学籍管理系统(php源码+mysql数据库)
- 新闻管理系统(原生php源码)
- php图书管理系统源码(附数据库)
- php文章管理系统源码(附数据库)
- PHP图书馆管理系统(源码 数据库脚本
- PHP考试答题系统
- 基于PHP的学生管理系统(优雅版)
- PHP物品管理系统
- php新闻管理系统源码
- resetful框架
- PHP免杀大马-过D盾过安全狗WAF免杀ph
- 友邻B2B系统(PHPB2B) 5.0.2 UTF-8
- 新型冠状病毒肺炎疫情实时图PHP源码
- PHP调用企业微信API接口底层类库
- 基于PHP的统一检索系统的设计与实现
- PHP版本的网络打印机提交打印代码
- conefor操作(Manual_Conefor_26)
- php Goto解密
- PHP大转盘抽奖
- php模拟文件上传(socket_upload.php)
- php连接mysql实现界面crud(附数据库文
- php在线相册(附数据库脚本)
- PHP在线人数统计
- php+Ajax无刷新分页列表
- PHP失物招领
- php 随机生成颜色
- php-redis中文帮助手册.chm
- php远程控制脚本
评论
共有 条评论