>php教程 >php手册 >[PHP] 소켓을 사용하여 Http 서비스 제공

[PHP] 소켓을 사용하여 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으로 문의하세요.