Maison > php教程 > php手册 > [PHP] 使用Socket提供Http服务

[PHP] 使用Socket提供Http服务

WBOY
Libérer: 2016-08-18 08:57:56
original
1253 Les gens l'ont consulté

我的SimpleLoader里面的一块

https://github.com/taoshihan1991/simpleloader

 

<span style="color: #000000;">php
namespace Server;

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Socket{
    </span><span style="color: #0000ff;">const</span> PORT=1024<span style="color: #000000;">;
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> create(){
        </span><span style="color: #800080;">$socket</span>=socket_create(AF_INET, SOCK_STREAM,<span style="color: #000000;"> SOL_TCP);
        socket_bind(</span><span style="color: #800080;">$socket</span>, "127.0.0.1",self::<span style="color: #000000;">PORT);
        socket_listen(</span><span style="color: #800080;">$socket</span>,4<span style="color: #000000;">);
        </span><span style="color: #0000ff;">echo</span> "start http://127.0.0.1:1024\r\n"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">while</span>(<span style="color: #0000ff;">true</span><span style="color: #000000;">) {
            </span><span style="color: #800080;">$msgsock</span> = socket_accept(<span style="color: #800080;">$socket</span><span style="color: #000000;">);
             </span><span style="color: #800080;">$buf</span> = socket_read(<span style="color: #800080;">$msgsock</span>,9024<span style="color: #000000;">);
            </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">preg_match</span>("/\/(.*) HTTP\/1\.1/",<span style="color: #800080;">$buf</span>,<span style="color: #800080;">$matchs</span><span style="color: #000000;">)){
                </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$matchs</span>[1]!="favicon.ico"<span style="color: #000000;">){
                    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$buf</span><span style="color: #000000;">;
                    </span><span style="color: #800080;">$_SERVER</span>['PATH_INFO']=<span style="color: #800080;">$matchs</span>[1] ? <span style="color: #800080;">$matchs</span>[1] : "Index/Index/index"<span style="color: #000000;">;
                    \SimpleLoader</span>::<span style="color: #000000;">router();
                    </span><span style="color: #800080;">$html</span>=\SimpleLoader::<span style="color: #008080;">pathInfo</span><span style="color: #000000;">();
                }
                socket_write(</span><span style="color: #800080;">$msgsock</span>,<span style="color: #800080;">$html</span><span style="color: #000000;">);
            }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
                socket_write(</span><span style="color: #800080;">$msgsock</span>,"hello world"<span style="color: #000000;">);
            }
             socket_close(</span><span style="color: #800080;">$msgsock</span><span style="color: #000000;">);
        } 
        socket_close(</span><span style="color: #800080;">$socket</span><span style="color: #000000;">);
    }
}</span>
Copier après la connexion

效果:

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal