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

歡迎訪問 生活随笔!

生活随笔

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

php

php7 获取数据流,stream_socket_accept()

發布時間:2023/12/2 php 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php7 获取数据流,stream_socket_accept() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

stream_socket_accept()

(PHP 5, PHP 7)

接受由stream_socket_server()創建的套接字連接

說明stream_socket_accept(resource$server_socket[,float$timeout= ini_get("default_socket_timeout")[,string&$peername]]):resource

接受由stream_socket_server()創建的套接字連接。

參數$server_socket需要接受的服務器創建的套接字連接。$timeout覆蓋默認的套接字接受的超時時限。輸入的時間需以秒為單位。$peername如果包含該參數并且是可以從選中的傳輸數據中獲取到,則將被設置給連接中的客戶端主機的名稱(地址)(怕出入很大,附帶上原文:Will be set to the name (address) of the client which connected, if included and available from the selected transport.)Note:

也可以之后通過stream_socket_get_name()來確定。

返回值

返回接受套接之后的資源流或者在失敗時返回FALSE。

注釋Warning

該函數不能被用于 UDP 套接字??梢允褂胹tream_socket_recvfrom()和stream_socket_sendto()來取而代之。

參見cURL 函數This code could be very helpfull...

The following code is for the "server". It listen for a message until CTRL-C

while (true)

{

// disconnected every 5 seconds...

receive_message('127.0.0.1','85',5);

}

function receive_message($ipServer,$portNumber,$nbSecondsIdle)

{

// creating the socket...

$socket = stream_socket_server('tcp://'.$ipServer.':'.$portNumber, $errno, $errstr);

if (!$socket)

{

echo "$errstr($errno)
\n";

}

else

{

// while there is connection, i'll receive it... if I didn't receive a message within $nbSecondsIdle seconds, the following function will stop.

while ($conn = @stream_socket_accept($socket,$nbSecondsIdle))

{

$message= fread($conn, 1024);

echo 'I have received that : '.$message;

fputs ($conn, "OK\n");

fclose ($conn);

}

fclose($socket);

}

}

?>

The following code is for the "client". It send a message, and read the respons...

send_message('127.0.0.1','85','Message to send...');

function send_message($ipServer,$portServer,$message)

{

$fp = stream_socket_client("tcp://$ipServer:$portServer", $errno, $errstr);

if (!$fp)

{

echo "ERREUR :$errno-$errstr
\n";

}

else

{

fwrite($fp,"$message\n");

$response = fread($fp, 4);

if ($response != "OK\n")

{echo 'The command couldn\'t be executed...\ncause :'.$response;}

else

{echo 'Execution successfull...';}

fclose($fp);

}

}

?>To check if there's a new connection waiting, without blocking, or (when using non-blocking mode) without notices), you can use stream_accept (as opposed to socket_select).

class GenericClass {

protected $resSocket=null;

function acceptConnections() {

# check that we still have a resource

if(is_resource($this->resSocket)) {

$arrRead=array($this->resSocket);

$arrWrite=array();

/** @warning Passing $arrRead,$arrWrite by reference */

if(stream_select($arrRead,$arrWrite,$arrWrite,0)) {

$resConnection=stream_socket_accept($this->resSocket,0);

# ... other stuff here

}

}

}

}

?>this function, compared to the function socket_accept, got an extra argument "timeout".

To make this function wait indefinitelly to incoming connections, just as in socket_accept, set timeout to -1. It works for me with PHP 5.0.4.Note that if you use 0 as timeout, the connection will timeout right away.To whom it may concern, and it may concern you greatly, stream_set_blocking has no effect on stream_socket_accept.

If you want it to return right away, connection or not, use 0 for the timeout parameter.

總結

以上是生活随笔為你收集整理的php7 获取数据流,stream_socket_accept()的全部內容,希望文章能夠幫你解決所遇到的問題。

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