?????????????????????????????????????е????????PHP??????????????????????????????????????????????????????PHP??????????????????????????????????????????????????????漰??Web???Daemon?????????Socket??????????????
????Socket????
????socket??????????“??”??“????”?????BSD UNIX???????????????????????????????”?????”??????????IP??????????????????????????Internet?????????????????????????????????????????????????Socket???????????????????????????????????
????????????????????
???????????socket??????????????????????????????
????PHP?е????
????###?????
????<?php
????set_time_limit(0);
????//????????????
????$host="127.0.0.1";
????$port=12387;
????//???????tcp??
????$socket=socket_create(AF_INET??SOCK_STREAM??SOL_TCP)
????or die("socket_create()failed:".socket_strerror(socket_last_error()));
????//??????????
????socket_set_block($socket)
????or die("socket_set_block()failed:".socket_strerror(socket_last_error()));
????//???????
????socket_bind($socket??$host??$port)
????or die("socket_bind()failed:".socket_strerror(socket_last_error()));
????//???????
????socket_listen($socket??4)
????or die("socket_listen()failed:".socket_strerror(socket_last_error()));
????echo"Binding the socket on$host:$port... ";
????while(true){
????//?????????????????????????Socket????????????????????????
????if(($msgsock=socket_accept($socket))<0){
????echo"socket_accept()failed:".socket_strerror(socket_last_error());
????}else{
????//??????
????$out='';
????while($buf=socket_read($msgsock??8192)){
????$out.=$buf;
????}
????//д????
????$in="??????$out";
????socket_write($msgsock??$in??strlen($in));
????}
????//???????
????socket_close($msgsock);
????}
????socket_close($socket);
?????>
?????????????????????2??裺
????????Socket??????????????
????????????????????????????????IO
????????????????????????
?????????д????????
?????????
????<?php
????set_time_limit(0);
????$host="127.0.0.1";
????$port=12387;
????//???????tcp??
????$socket=socket_create(AF_INET??SOCK_STREAM??SOL_TCP)
????or die("socket_create()failed:".socket_strerror(socket_last_error()));
????echo"try to connect to$host:$port... ";
????$result=socket_connect($socket??$host??$port)
????or die("socket_connect()failed:".socket_strerror(socket_last_error()));
????$in="hello ";
????if(!socket_write($socket??$in??strlen($in))){
????echo"socket_write()failed:".socket_strerror($socket);
????}else{
????echo"???????? ";
????}
????$out='';
????while($buf=socket_read($socket??8192)){
????$out.=$buf;
????}
????echo"???????????$out ";
????socket_close($socket);
?????>
?????????????????2??裺
??????????????Socket
??????????д????
?????????????????????