PHP Socket網(wǎng)絡(luò)操作類定義與用法示例
本文實例講述了PHP Socket網(wǎng)絡(luò)操作類定義與用法。分享給大家供大家參考,具體如下:
web前端測試:
<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()">關(guān)閉服務(wù)</button>
</body>
</html>
mysocket.php:
<?php
class Connector
{
public static $instance=null;
public $conn;
private function __construct()
{
set_time_limit(0);
$ip = '192.168.238.1';
$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;
}
}
}
}
?>
main.php(調(diào)用場所):
<?php
require_once('mysocket.php');
$con = Connector::getInstance();
$req = $_GET['cmd'];
$con->sendMsg($req);
$ret = $con->getMsg();
echo $ret;
?>
應(yīng)用說明:
客戶端ajax發(fā)起請求調(diào)用Php,然后php借助socket進一步發(fā)起請求給C++模塊。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php socket用法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)學(xué)運算技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》及《PHP網(wǎng)絡(luò)編程技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP查找數(shù)組中只出現(xiàn)一次的數(shù)字實現(xiàn)方法【查找特定元素】
這篇文章主要介紹了PHP查找數(shù)組中只出現(xiàn)一次的數(shù)字實現(xiàn)方法,涉及php使用array_count_values針對數(shù)組元素進行統(tǒng)計的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
PHP替換Word中變量并導(dǎo)出PDF圖片的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于PHP替換Word中變量并導(dǎo)出PDF圖片的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
從一個不錯的留言本弄的mysql數(shù)據(jù)庫操作類
本文通過實例代碼給大家介紹了mysql數(shù)據(jù)庫操作類的相關(guān)知識,感興趣的朋友跟隨腳本之家小編一起看看吧2007-09-09
在WordPress中實現(xiàn)評論頭像的自定義默認和延遲加載
這篇文章主要介紹了在WordPress中實現(xiàn)評論頭像的自定義默認和延遲加載,需要的朋友可以參考下2015-11-11
php中使用key,value,current,next和prev函數(shù)遍歷數(shù)組的方法
這篇文章主要介紹了php中使用key,value,current,next和prev函數(shù)遍歷數(shù)組的方法,較為詳細的分析了php中數(shù)組遍歷的常用技巧與實例用法,需要的朋友可以參考下2015-03-03
使用PHPStorm+XDebug搭建單步調(diào)試環(huán)境
由于 PhpStorm 不像 Zend 公司為 Zend Studio 那樣配套集成了很多開發(fā)部件,包括解釋器、調(diào)試器、虛擬機、服務(wù)器、開發(fā)框架等等。因此,配置 PhpStorm 開發(fā)環(huán)境相對較繁瑣,有很多需要注意的地方。2017-11-11

