php中socket的執行

2021-07-22 16:42:55 字數 2030 閱讀 1979

服務端:

<?php

//確保在連線客戶端時不會超時

set_time_limit(0);

$ip = '127.0.0.1';

$port = 1234;

/* +-------------------------------

* @socket通訊整個過程

+-------------------------------

* @socket_create

* @socket_bind

* @socket_listen

* @socket_accept

* @socket_read

* @socket_write

* @socket_close

+--------------------------------

*//*---------------- 以下操作都是手冊上的 -------------------*/

if(($sock = socket_create(af_inet,sock_stream,sol_tcp)) < 0)

if(($ret = socket_bind($sock,$ip,$port)) < 0)

if(($ret = socket_listen($sock,4)) < 0)

var_dump($sock);

$changes=array($sock);

$write=null;

$except=null;

socket_select($changes,$write,$except,null);

//var_dump($changes);

$sock=$changes[0];

$count = 0;

if (($msgsock = socket_accept($sock)) < 0) else ;

}//echo $buf;

socket_close($msgsock);

socket_close($sock);

?>

客戶端:

<?php

error_reporting(e_all);

set_time_limit(0);

echo "\n";

$port = 1234;

$ip = "127.0.0.1";

/* +-------------------------------

* @socket連線整個過程

+-------------------------------

* @socket_create

* @socket_connect

* @socket_write

* @socket_read

* @socket_close

+--------------------------------

*/$socket = socket_create(af_inet, sock_stream, sol_tcp);

if ($socket < 0) else

echo "試圖連線 '$ip' 埠 '$port'...\n";

$result = socket_connect($socket, $ip, $port);

if ($result < 0) else

$in = "ho\r\n";

$in .= "first blood\r\n";

$out = '';

if(!socket_write($socket, $in, strlen($in))) else

while($out = socket_read($socket, 8192))

echo "關閉socket...\n";

socket_close($socket);

echo "關閉ok\n";

?>

php中socket的使用

一 開啟socket phpinfo 檢視是否開啟了socket擴充套件,否則在php.ini中開啟。error reporting e all set time limit 0 ob implicit flush address 127.0.0.1 port 10005 建立埠 建立乙個socke...

php中socket的使用(重點參考)

一 開啟socket phpinfo 檢視是否開啟了socket擴充套件,否則在php.ini中開啟。二 伺服器端 的寫法 error reporting e all set time limit 0 ob implicit flush address 127.0.0.1 port 10005 建立...

php中socket的使用(重點參考)

一 開啟socket phpinfo 檢視是否開啟了socket擴充套件,否則在php.ini中開啟。二 伺服器端 的寫法 error reporting e all set time limit 0 ob implicit flush address 127.0.0.1 port 10005 建立...