Application Example of PHP socket (fsockopen) function
Using the Fsockopen function, the first thing to do is to open the allow_url_open=on in php.ini;
Fsockopen is the encapsulation of the socket client code, which encapsulates the socket_create (), Socket_connect ().
Server-side code: server.php
"; Socket_write ($msgsock, $msg, strlen ($msg)); Returns the message to the client echo "read client message\n"; $buf = Socket_read ($msgsock, 8192); Gets the message sent by the client $talkback = "2.received message: $buf \ n"; echo $talkback; if (false = = = Socket_write ($msgsock, $talkback, Strlen ($talkback))) {//return information to the client echo "Socket_write () failed reason:". Socket_strerror (Socket_last_error ($sock)). " \ n ";} else {echo "Send Success";} Socket_close ($msgsock);} Socket_close ($sock);
Client code: fsocket.php
n ";} else {$in = "head/http/1.1\r\n"; $in. = "host:localhost \ r \ n"; $in. = "connection:close\r\n\r\n"; fputs ($fp, $in); while ( !feof ($FP)) {echo fgets ($fp, 128);} Fclose ($FP);}? >