Home >php教程 >php手册 >[PHP] Use Socket to provide Http service

[PHP] Use Socket to provide Http service

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-08-18 08:57:561285browse

A piece of my 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>

Effect:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn