【php socket通讯】php实现http服务.docx
文本预览下载声明
PAGE
PAGE 1
【php socket通讯】php实现http服务
http服务是建立在tcp服务之上的,它是tcp/ip协议的应用,前面我们已经实现了tcp服务,并且用法三种不同的方式衔接tcp服务php中衔接tcp服务的三种方式既然http也是tcp应用层的一种,那么我们挺直用法扫瞄器来衔接tcp服务可不行以?答案是可以的,只不过衔接之后挺直返回给扫瞄器的信息,扫瞄器不能够正确的识别出来。那么怎么才干让扫瞄器正确的识别tcp服务返回的信息呢?这个时候我们就需要用法到http协议啦,至于http传输中都传了哪些信息可以在扫瞄器中f12查看名目结构:http_serv.php文件39;video/x-msvideo39;,39;bmp39;=39;image/bmp39;,39;css39;=39;text/css39;,39;doc39;=39;application/msword39;,39;gif39;=39;image/gif39;,39;htm39;=39;text/html39;,39;html39;=39;text/html39;,39;ico39;=39;image/x-icon39;,39;jpe39;=39;image/jpeg39;,39;jpeg39;=39;image/jpeg39;,39;jpg39;=39;image/jpeg39;,39;js39;=39;application/x-javascript39;,39;mpeg39;=39;video/mpeg39;,39;ogg39;=39;application/ogg39;,39;png39;=39;image/png39;,39;rtf39;=39;text/rtf39;,39;rtx39;=39;text/richtext39;,39;swf39;=39;application/x-shockwave-flash39;,39;wav39;=39;audio/x-wav39;,39;wbmp39;=39;image/vnd.wap.wbmp39;,39;zip39;=39;application/zip39;,);/***@paramstring$host监听地址*@paramint$port监听端口*@paramstring$_root网站根名目*/publicfunction__construct($host,$port,$_root){$this-host=$host;$this-port=$port;$this-_root=$_root;}/***启动http服务*/publicfunctionstart(){//创建socket套接字$socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);//设置堵塞模式socket_set_block($socket);//为套接字绑定ip和端口socket_bind($socket,$this-host,$this-port);//监听socketsocket_listen($socket,4);while(true){//接收客户端哀求if(($msgsocket=socket_accept($socket))!==false){//读取哀求内容$buf=socket_read($msgsocket,9024);preg_match(quot;/\/(.*)HTTP\/1\.1/quot;,$buf,$matchs);preg_match(quot;/Accept:(.*?),/quot;,$buf,$matchss);//猎取接收文件类型$type=explode(quot;/quot;,$matchss[1])[0];if($type==quot;textquot;){$content=$this-GetString($matchs[1]);}else{$content=$this-GetImg($matchs[1]);}socket_write($msgsocket,$content,strlen($content));socket_close($msgsocket);}}}/***组装消息头信息模板*@paramint$code状态码*@paramstring$status状态名称*@paramstring$content发
显示全部