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

[PHP] 使用Socket提供Http服务

WBOY
Lepaskan: 2016-08-18 08:57:56
asal
1252 orang telah melayarinya

我的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>
Salin selepas log masuk

效果:

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan