PHP socket network operation class

不言
Release: 2023-03-22 22:42:02
Original
1226 people have browsed it

This article shares with you the code of the socket network operation class in php. Friends who are interested can take a look at the content of this article

## Web front-end testing


<html>
<head>
	<title>test</title>
	<script>
	g_xmlHttpReq = new XMLHttpRequest();
	function onReplyCallback()
	{
		if(g_xmlHttpReq.readyState==4 && g_xmlHttpReq.status==200)
		{
			alert(g_xmlHttpReq.responseText);
		}
	}
	function on_stop_service()
	{
		var cmd = document.getElementById("inCmd").value;
		g_xmlHttpReq.open("GET","./service/main.php?cmd=" + cmd,true);
		g_xmlHttpReq.onreadystatechange=onReplyCallback;
        g_xmlHttpReq.send(null);
	}
	</script>
</head>

<body>
<input type="text" id="inCmd">
<hr>
<button onclick="on_stop_service()">关闭服务</button>
</body>
</html>
Copy after login





# mysocket.php


<?php

class Connector
{
	public static $instance=null;
	public $conn;
	
	private function __construct()
	{
			set_time_limit(0);
			$ip = &#39;192.168.238.1&#39;;
			$port = 8888;
			
			if(($this->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;
			}
		}
	}

}



?>
Copy after login


## main.php (calling place)



<?php
require_once(&#39;mysocket.php&#39;); 

$con = Connector::getInstance();
$req = $_GET[&#39;cmd&#39;];
$con->sendMsg($req);
$ret = $con->getMsg();
echo $ret;
?>
Copy after login


## Application Description


The client ajax initiates a request to call Php, and then PHP uses the socket to further initiate a request to the C++ module.

Related recommendations:

Detailed explanation of SOCKET programming in PHP

Connection settings for websocket in php on MAC

Detailed explanation of socket communication in php


The above is the detailed content of PHP socket network operation class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!