首頁 >php教程 >php手册 >[PHP] 使用Socket提供Http服務

[PHP] 使用Socket提供Http服務

WBOY
WBOY原創
2016-08-18 08:57:561236瀏覽

我的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>

效果:

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn