2019-02-15 10:45:54  538338 8

php网络编程之http服务器以及websocket网页聊天实战

 标签:   

socket通信简单的demo

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

server.php

socket_set_option($socket,SOL_SOCKET,SO_REUSEADDR,true);

socket_bind($socket,0,8888);

socket_listen($socket);

while(true){

        $conSock = socket_accept($socket);

        socket_getpeername($conSock,$ip,$port);

        echo  'ip:'.$ip.'...port:'.$port.'...connetted'.PHP_EOL;

        while(true){
                $recMsg =  socket_read($conSock,1024);

                socket_write($conSock,strtoupper($recMsg),strlen($recMsg));

                echo $recMsg ;

        }
}


client.php

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

socket_connect($socket,'192.168.113.136',8888);

while(true){
    fwrite(STDOUT,'请输入内容:');

    $in = fgets(STDIN);

    socket_write($socket,$in,strlen($in));

    echo socket_read($socket,1024);
}


先运行 server.php    


1.jpg


在运行 client.php


2.jpg


实现了socket的通信,但是这里有个问题,当我们在开一个终端运行 client.php时,这时 无法进行 socket通信,因为 上一个client.php在阻塞,没办法进行应答。这里就需要一个新的知识点,来解决。


33.jpg


IO阻塞模型只能是同一个时刻只能由一个客户端进行访问。不能好几个客户端同时访问服务端。

解决:

1 多进程

2 IO多路复用机制

select(apache)   epoll (nginx) 模型

socket_select() 函数 就是使用了 select 模型 实现io多路复用  

3.jpg

socket_select 这个函数解决


//实现io多路复用 返回活跃的连接
socket_select ( array &$read , array &$write , array &$except , int $tv_sec [, int $tv_usec = 0 ] )
 
注意 :前三个参数都是引用传值。
 
$read  服务端监听的套接字资源,当它有变化(就是有新消息到或者有客户端连接/断开)时,socket_select函数才会返回,继续往下执行。
 
$write是监听是否有客户端写数据,传入NULL是不关心是否有写变化。
 
$except是$sockets里面要被排除的元素,传入NULL是”监听”全部。
 
第四个参数为null为阻塞, 为0位非阻塞, 为 >0 为等待时间
 

返回 活跃的链接数  当有连接 或数据操作时就会返回


改造一下sever.php的代码


//server.php  把上面的server.php 改造之后

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

socket_set_option($socket,SOL_SOCKET,SO_REUSEADDR,true);

socket_bind($socket,0,8888);

socket_listen($socket);

$sockets[] = $socket;
$write = null;
$except = null;


while(true){

        $tmp_sockets = $sockets;

        socket_select($tmp_sockets,$write,$except,null);

        foreach($tmp_sockets as $sock){
                if($sock==$socket){
                        $conSock = socket_accept($socket);
                        socket_getpeername($conSock,$ip,$port);
                        echo  'ip:'.$ip.'...port:'.$port.'...connetted'.PHP_EOL;
                        $sockets[] = $conSock;
                }else{
                        $recMsg =  socket_read($sock,1024);
                        socket_write($sock,strtoupper($recMsg),strlen($recMsg));
                        echo $recMsg ;

                }

        }
}


client.php代码不变  然后 在分别执行  server.php  以及  client.php


4-1.jpg4-2.jpg


两个终端链接 可以进行通信 不受影响


实现web服务器功能


$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

socket_set_option($socket,SOL_SOCKET,SO_REUSEADDR,true);

socket_bind($socket,0,80);

socket_listen($socket);

$sockets[] = $socket;
$write = null;
$except = null;


while(true){

        $tmp_sockets = $sockets;

        socket_select($tmp_sockets,$write,$except,null);

        foreach($tmp_sockets as $sock){
                if($sock==$socket){
                        $conSock = socket_accept($socket);
                        socket_getpeername($conSock,$ip,$port);
                        $sockets[] = $conSock;
                }else{
                        $str = 'hello this is socket';

                        $len = strlen($str);

                        $response  = "HTTP/1.1 200 OK\r\n";
                        $response .= "Content-Type: text/html; charset=utf-8\r\n";
                        $response .="Content-Length: $len\r\n\r\n";

                        $response .=$str;


                        @socket_write($sock,$response,strlen($response));


                }

        }
}


浏览器访问

5.jpg


已经可以正常的能够访问了,不依赖于 nginx apache 就可以直接访问,比较简陋 但是大体功能实现了。仅供学习参考。


8 条留言

  1. 匿名用户

    can you order viagra without a prescription for whom is viagra contraindicated <a href=" https://pharm-usa-official.com/# #">viagra samples </a> how often can you take viagra teva pharmaceuticals generic viagra jodi west viagra viagra 100mg <a href=https://pharm-usa-official.com/#>female viagra pills </a> joel and lauren viagra prank is viagra and pomegranate safe https://xn--kckzb836wvegvt1b.com/%e3%80%8cx1%e3%80%8d%e5%87%ba%e8%ba%ab%e3%82%a4%e3%83%bb%e3%83%8f%e3%83%b3%e3%82%ae%e3%83%a7%e3%83%ab%e3%81%ae%e7%94%9f%e3%81%84%e7%ab%8b%e3%81%a1/?unapproved=25&moderation-hash=5a9117fb3df743550a67745ee8dfba7d#comment-25 https://sylvastallone.com/jamb-syllabus-for-biology-download-pdf-now/?unapproved=3750&moderation-hash=e5beb3a55d37c3a816859fa58a7bcdf7#comment-3750 https://en.brinkwire.com/news/nw-syria-148000-civilians-displaced-in-six-days/?unapproved=1162&moderation-hash=148d4667d08b877c35ab99f54ca5e4a3#comment-1162 https://rsmasmitra.co.id/ketahui-mitos-atau-fakta-kehamilan/?unapproved=804&moderation-hash=e2879e8bfeb08563aad68752748a0e54#comment-804 https://blackstreetmedia.com/2020/01/03/sisters-in-style-beyonce-gifts-tiffany-haddish-a-custom-suit-right-off-her-back/?unapproved=52551&moderation-hash=7bd1e6ffcb1efdd15d4b7fd74eb038ef#comment-52551

  2. 匿名用户

    canadian viagra typical age viagra <a href=" https://pharm-usa-official.com/# #">over the counter viagra </a> can i buy teva viagra from europe does viagra effect ejaculation? what presidential candidate was the first spokesman for viagra? viagra connect <a href=https://pharm-usa-official.com/#>cialis vs viagra </a> stepsis makes stepbro's viagra boner won't go away #ИМЯ? http://bbs.mumayi.net/space-uid-8237097.html https://www.iestork.org/coronavirus-chaos-continues-spain-declares-state-of-emergency-ie-students-stranded-worldwide-sg-elections-postponed-indefinitely/?unapproved=2126&moderation-hash=010da73d77441a6f8ccccddc71d365ca#comment-2126 http://100freechip.com/no-deposit-casinos/comment-page-1/?unapproved=563709&moderation-hash=97b592c5bbc791f07698ff1c04f60789#comment-563709 http://www.apostando.org/probabilidades-en-el-poker/?unapproved=34209&moderation-hash=d52db11c0d7bf734fa4ca277de617afd#comment-34209 https://novusbars.com/32-chinese-dominoes-pai-gow/?unapproved=224&moderation-hash=75f66164a49b950180e1575653c29fb7#comment-224

  3. 匿名用户

    buy viagra viagra pills <a href="http://vidiomir.ru/ #">generic viagra </a> viagra online viagra 100mg viagra online viagra generic <a href=https://combinedparkingsolutions.com/terms-conditions/>viagra generic </a> generic viagra viagra generic

  4. 匿名用户

    generic viagra viagra generic <a href="http://babyandmomtimes.com/category/baby-registry-finder/ #">generic viagra </a> buy viagra viagra generic ufiouas0209sss buy viagra viagra generic <a href=http://wdettv.org/>viagra buy </a> buy viagra generic viagra

  5. 匿名用户

    cheap viagra viagra pills <a href="http://www.biovedia.ru #">viagra buy </a> viagra 100mg viagra pills ufiouas0209sss viagra 100mg buy viagra <a href=http://frafoot-statti.ru/>viagra generic </a> buy viagra viagra pills

给我留言

评论内容