无聊写的一个PHP Socket类
生活随笔
收集整理的這篇文章主要介紹了
无聊写的一个PHP Socket类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
無聊寫的一個PHP Socket類,功能還比較簡單,不完善.
<?php
/**?*//***
?????*?@project:socket類
?????*?@license:GPL
?????*?@author:?8th@live.cn-星期八
?????*?@description:PHP?Socket協議異步通信
?????*?@file:socket.class.php
?????*?@created?date:Fri?Apr?25?12:22:33?CST?2008
?????*?@last?modified?:
?????***/
class?sockets
{
????public?$host;//通信地址
????public?$port;//通信端口
????public?$limitTime=0;//連接超時時間
????public?$backlog=3;//請求隊列中允許的最大請求數
????private?$socket=null;
????private?$result=null;
????private?$spawn=null;
????private?$input=null;
????//構造函數
????public?function?__construct()
????{
????????set_time_limit($this->limitTime);
????????$this->socket?=?socket_create(AF_INET,?SOCK_STREAM,?0)?or?die("Could?not?create?socket\n");
????}
????//創建Socket連接,監聽外部連接
????public?function?socket_bind_listen()
????{
????????$this->result?=?socket_bind($this->socket,?$this->host,?$this->port)?or?die("Could?not?bind?to?socket\n");
????????$this->result?=?socket_listen($this->socket,?$this->backlog)?or?die("Could?not?set?up?socket?listener\n");
????}
????//接受請求連接,調用socket處理信息
????public?function?accept_client()
????{
????????$this->spawn?=?socket_accept($this->socket)?or?die("Could?not?accept?incoming?connection\n");
????????$msg?=?"Welcome?to?the?Test?Server";
????????socket_write($this->spawn,?$msg,?strlen($msg));
????}
????//?讀取客戶端輸入?
????public?function?read_client()
????{
????????$this->input?=?socket_read($this->spawn,?2048,?PHP_NORMAL_READ)?or?die("Could?not?read?input\n");?
????}
????//?處理客戶端輸入并返回數據?
????public?function?write_client()
????{
????????socket_write($this->spawn,?$this->input,?strlen?($this->input))?or?die("Could?not?write?output\n");
????}
????//析構函數
????public?function?__destruct()
????{
????????socket_close($this->spawn);?
????????socket_close($this->socket);?
????}
}
/**?*//***
$socket_test=new?sockets();
$socket_test->host="192.168.0.2";
$socket_test->port="13654";
$socket_test->socket_bind_listen();
$socket_test->accept_client();
$socket_test->read_client();
$socket_test->write_client();
?????***/
?>
<?php
/**?*//***
?????*?@project:socket類
?????*?@license:GPL
?????*?@author:?8th@live.cn-星期八
?????*?@description:PHP?Socket協議異步通信
?????*?@file:socket.class.php
?????*?@created?date:Fri?Apr?25?12:22:33?CST?2008
?????*?@last?modified?:
?????***/
class?sockets
{
????public?$host;//通信地址
????public?$port;//通信端口
????public?$limitTime=0;//連接超時時間
????public?$backlog=3;//請求隊列中允許的最大請求數
????private?$socket=null;
????private?$result=null;
????private?$spawn=null;
????private?$input=null;
????//構造函數
????public?function?__construct()
????{
????????set_time_limit($this->limitTime);
????????$this->socket?=?socket_create(AF_INET,?SOCK_STREAM,?0)?or?die("Could?not?create?socket\n");
????}
????//創建Socket連接,監聽外部連接
????public?function?socket_bind_listen()
????{
????????$this->result?=?socket_bind($this->socket,?$this->host,?$this->port)?or?die("Could?not?bind?to?socket\n");
????????$this->result?=?socket_listen($this->socket,?$this->backlog)?or?die("Could?not?set?up?socket?listener\n");
????}
????//接受請求連接,調用socket處理信息
????public?function?accept_client()
????{
????????$this->spawn?=?socket_accept($this->socket)?or?die("Could?not?accept?incoming?connection\n");
????????$msg?=?"Welcome?to?the?Test?Server";
????????socket_write($this->spawn,?$msg,?strlen($msg));
????}
????//?讀取客戶端輸入?
????public?function?read_client()
????{
????????$this->input?=?socket_read($this->spawn,?2048,?PHP_NORMAL_READ)?or?die("Could?not?read?input\n");?
????}
????//?處理客戶端輸入并返回數據?
????public?function?write_client()
????{
????????socket_write($this->spawn,?$this->input,?strlen?($this->input))?or?die("Could?not?write?output\n");
????}
????//析構函數
????public?function?__destruct()
????{
????????socket_close($this->spawn);?
????????socket_close($this->socket);?
????}
}
/**?*//***
$socket_test=new?sockets();
$socket_test->host="192.168.0.2";
$socket_test->port="13654";
$socket_test->socket_bind_listen();
$socket_test->accept_client();
$socket_test->read_client();
$socket_test->write_client();
?????***/
?>
轉載于:https://www.cnblogs.com/8th/archive/2008/04/23/socket.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的无聊写的一个PHP Socket类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Domino+Qucikplace+Sa
- 下一篇: Microsoft uaa bus dr