国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP Socket網絡操作類定義與用法示例

2024-05-04 23:59:53
字體:
來源:轉載
供稿:網友

本文實例講述了PHP Socket網絡操作類定義與用法。分享給大家供大家參考,具體如下:

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()">關閉服務</button></body></html>

mysocket.php:

<?phpclass 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(調用場所):

<?phprequire_once('mysocket.php');$con = Connector::getInstance();$req = $_GET['cmd'];$con->sendMsg($req);$ret = $con->getMsg();echo $ret;?>

應用說明:

客戶端ajax發起請求調用Php,然后php借助socket進一步發起請求給C++模塊。

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 林周县| 万年县| 岳池县| 梅州市| 延长县| 榕江县| 新巴尔虎左旗| 九龙坡区| 德保县| 北票市| 钟祥市| 织金县| 文安县| 牙克石市| 自治县| 施甸县| 柳河县| 泽库县| 日土县| 额尔古纳市| 普洱| 页游| 科技| 出国| 赤峰市| 柞水县| 宁波市| 即墨市| 宽城| 志丹县| 浦北县| 宁南县| 碌曲县| 江阴市| 资源县| 洪江市| 涡阳县| 含山县| 乳源| 綦江县| 临潭县|