This article mainly introduces the definition and usage of the PHP Socket network operation class. It analyzes the definition of the PHP Socket network operation class in the form of examples and related usage skills such as sending query requests in combination with ajax. Friends in need can refer to it
The examples in this article describe the definition and usage of PHP Socket network operation classes. Share it with everyone for your reference, the details are as follows:
web front-end test:
##
mysocket.php:
conn = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) { echo "socket_create() 失败的原因是:".socket_strerror($this->conn)."\n"; } $result = socket_connect($this->conn, $ip, $port); if ($result < 0) { echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n"; }else { echo "连接OK\n"; } } public static function getInstance() { if(is_null(self::$instance)) { self::$instance = new Connector; } return self::$instance; } public function sendMsg($msg) { socket_write($this->conn,$msg); } public function getMsg() { $clients = array($this->conn); while(true) { $read = $clients; $wrSet = NULL; $errSet = NULL; if(socket_select($read, $wrSet,$errSet, 3) < 1) { continue; } foreach($read as $read_sock) { $data = @socket_read($read_sock,1024,PHP_BINARY_READ); socket_close($this->conn); return $data; } } } } ?>
main.php (calling place):
sendMsg($req); $ret = $con->getMsg(); echo $ret; ?>
Application description:
The client initiates an ajax request to call Php, and then PHP uses the socket to further initiate a request to the C++ module.The above is the detailed content of Examples explaining the definition and usage of PHP Socket network operation classes. For more information, please follow other related articles on the PHP Chinese website!