日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

无聊写的一个PHP Socket类

發布時間:2024/4/15 php 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 无聊写的一个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();
?????**
*/

?>

轉載于:https://www.cnblogs.com/8th/archive/2008/04/23/socket.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的无聊写的一个PHP Socket类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。