in the past, there is no low-level socket frame reception function in PHP, it seems to have not looked carefully, but those instructions are too few, (more exciting in the English version of the example of the case there is a sentence: This program can not run, because no listen function, but in the Chinese version is not, The translator is not responsible for it. Int Socket_recv (ResourceSocketstring&buf, int len,int flags)ResourceSocket is a generated socketstring&BUF is the receive buffer int len is the length you intend to receive INT flags is a flag0x1data should be sent out of band, so-called out-of-band data is TCP emergency data0x2copies the useful data into the buffer, but does not delete it from within the system buffer. 0x4do not route the package out. Above three items with sock.h file defined exactly the same0x8Data Integrity Record0x100data complete processing when socket_read in Php_binary_read mode (default, but generally with php_normal_read) is basically the same as socket_recv below is a small example of two Tests sent to receive method:>>CODE/*server sock_accept* client = filename* server <= filesize (filename) * client = "OK" * Server <= File_g et_contents (filename) * client = "FIN" * Server sock_close** NOTE: Here with flags for 0x2 the buffer will receive so read the data*/Server.PHP>>CODE<?PHP/*server sock_accept* client = filename* server <= filesize (filename) * client = "OK" * Server <= File_g et_contents (filename) * client = "FIN" * Server sock_close** Read buffer Save all data that it has recived ever.*/$flag= 0x2;error_reporting(E_all);/*Allow the "script to" around waiting for connections.*/Set_time_limit(0);/*Turn on implicit output flushing so we see what we ' re getting* as it comes in.*/Ob_implicit_flush();$address= ' 127.0.0.1 ';$port= 10000;if(($sock= Socket_create (Af_inet, Sock_stream, sol_tcp) < 0) {Echo"Socket_create () Failed:reason:". Socket_strerror ($sock) . "\ n";Exit;}if(($ret= Socket_bind ($sock,$address,$port)) < 0) {Echo"Socket_bind () Failed:reason:". Socket_strerror ($ret) . "\ n";Exit;}if(($ret= Socket_listen ($sock, 5)) < 0) {Echo"Socket_listen () Failed:reason:". Socket_strerror ($ret) . "\ n";Exit;} Do {if(($msgsock= Socket_accept ($sock)) < 0) {Echo"Socket_accept () Failed:reason:". Socket_strerror ($msgsock) . "\ n"; Break;}Else{Echo"Accept \ r \ n";} Do {$p= 0;if(($ret= Socket_recv ($msgsock,$buf, 1024,$flag)) ==0 ) {Echo"$bufSocket_read () failed reason: ". Socket_strerror ($ret) . "\ n"; Break;//2;}$msg=substr($buf,$p);$p+=strlen($msg);EchoR$msg\ n ";if(file_exists($msg)){$fcon=file_get_contents($msg);$flen=filesize($msg);if( ($ret= Socket_write ($msgsock,$flen,strlen($flen))) !=strlen($flen)){EchoThe Write Error$ret\ n "; Break;//2;}EchoS$flen\ n ";if(($ret= Socket_recv ($msgsock,$buf, 1024,$flag)) ==0 ) {Echo"$bufSocket_read () failed reason: ". Socket_strerror ($ret) . "\ n"; Break;//2;}$msg=substr($buf,$p);$p+=strlen($msg);EchoR$msg\ n ";$sndlen= 0; Do{$sndlen+ = Socket_write ($msgsock,$fcon,$flen-$sndlen);Echo"------[".$sndlen."] [$flen]\n ";} while($sndlen<$flen); Echo"S:!!!!! \ n "; Do{socket_recv ($msgsock,$buf, 1024,$flag);if(substr($buf, -3) = = "FIN"){Echo"Fin\n"; Break2;}} while(true);}Else{socket_write ($msgsock, "Nofo", 4);Echo"File not found\n"; Break;}} while(true); Socket_close ($msgsock);} while(true); Socket_close ($sock);?>Client.PHP>>CODE<?PHP//error_reporting (e_all);$flag= 0x2;Echo"TCP/IP Connection:";/*Get the port for the WWW service.*/$service _port= 10000;//getservbyname (' www ', ' tcp ');/*Get The IP address for the target host.*/$address= ' 127.0.0.1 ';//gethostbyname (' www.example.com ');/*Create A TCP/IP socket.*/$socket= Socket_create (Af_inet, Sock_stream,sol_tcp);if($socket< 0) {Echo"Socket_create () Failed:reason:". Socket_strerror ($socket) . "\ n";Exit;} Else {Echo"Ok.\n";}Echo"Attempting to connect to"$address' On port '$service _port‘...";$result= Socket_connect ($socket,$address,$service _port);if($result< 0) {Echo"Socket_connect () Failed.\nreason: ($result) " . Socket_strerror ($result) . "\ n";Exit;} Else {Echo"Ok.\n";}$in= "server.php";Echo"S:getfile$in.";if(Socket_write ($socket,$in,strlen($in)) ==0){Echo"Err\n";return;}Echo"Ok\n";if(($ret= Socket_recv ($socket,$out, 2048,$flag)) = = 0) {Echo"Error read[".$out."] \ n ";}Echo"<=:$out\ n ";if($out!== "Nofo"){Echo"=>:ok ...";if(Socket_write ($socket, "OK", 2) ==0 ){Echo"Err\n";return;}Echo"Ok\n";$len=strlen($out)+$out;$l= 0; while($len>$l) {SOCKET_RECV ($socket,$t,$len,$flag);$l=strlen($t);Echo"----[$l][$len]\n ";}Echo"<====:\n[".substr($t,strlen($out))."] \ n ";Echo"=>:ok ...";if(Socket_write ($socket, "FIN", 3) ==0 ){Echo"Err\n";return;}Echo"Ok\n";}Else{Echo"FILE not FOUND on server\n";}//Socket_close ($socket);Sleep(2);Echo"Ok.\n\n";?>
The Socket_recv method in PHP explains