PHP實現(xiàn)Socket服務(wù)器的代碼
更新時間:2008年04月03日 19:08:47 作者:
PHP實現(xiàn)Socket服務(wù)器的代碼
<?php
ob_implicit_flush();
set_time_limit(0);
$address = "192.40.7.93";//換成你自己的地址
$port = 10000;
if(($socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) == false)
echo "錯誤(socket_create):".socket_strerror(socket_last_error())."<br />";
if(socket_bind($socket,$address,$port) == false)
echo "錯誤(socket_bind):".socket_strerror(socket_last_error())."<br />";
if(socket_listen($socket) == false)
echo "錯誤(socket_listen):".socket_strerror(socket_last_error())."<br />";
/*
After the socket socket has been created using socket_create() and bound to a name with socket_bind(),
it may be told to listen for incoming connections on socket.
*/
while(true){
if(($msgSocket = socket_accept($socket)) == false){
echo "錯誤(socket_accept):".socket_strerror(socket_last_error())."<br />";
break;
}
/*
this function will accept incoming connections on that socket.
Once a successful connection is made, a new socket resource is returned, which may be used for communication.
If there are multiple connections queued on the socket, the first will be used.
If there are no pending connections, socket_accept() will block until a connection becomes present.
If socket has been made non-blocking using socket_set_blocking() or socket_set_nonblock(), FALSE will be returned.
*/
$msg = "Welcome!<br />";
//socket_write($msg,$msg,strlen($msg));
$command = "";
while(true){
if(($buf = socket_read($msgSocket,2048,PHP_BINARY_READ)) == false){
echo "錯誤(socket_read):".socket_strerror(socket_last_error())."<br />";
break 2;
}
/*
The function socket_read() reads from the socket resource socket created by the socket_create() or socket_accept() functions.
The maximum number of bytes read is specified by the length parameter.
Otherwise you can use \r, \n, or \0 to end reading (depending on the type parameter, see below).
*/
/*
if(!$buf = trim($buf))
continue; // ????
if($buf == "quit")
break;
if($buf == "shutdown"){
socket_close($msgSocket);
break 2;
}
$tallBack = "You say:$buf\n";
socket_write($msgSocket,$tallBack,strlen($tallBack));
*/
if(ord($buf) != 13)
$command .= $buf;
else{
$command1 = "You Say:$command\r\n";
socket_write($msgSocket,$command1,strlen($command1));
echo "User typed:".$command."<br />";
$command = "";
}
}
socket_close($msgSocket);
}
socket_close($socket);
?>
然后打開CMD,輸入:telnet 192.40.7.93 10000,自己體驗去吧!

注,要把:php_sockets.dll 打開
ob_implicit_flush();
set_time_limit(0);
$address = "192.40.7.93";//換成你自己的地址
$port = 10000;
if(($socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) == false)
echo "錯誤(socket_create):".socket_strerror(socket_last_error())."<br />";
if(socket_bind($socket,$address,$port) == false)
echo "錯誤(socket_bind):".socket_strerror(socket_last_error())."<br />";
if(socket_listen($socket) == false)
echo "錯誤(socket_listen):".socket_strerror(socket_last_error())."<br />";
/*
After the socket socket has been created using socket_create() and bound to a name with socket_bind(),
it may be told to listen for incoming connections on socket.
*/
while(true){
if(($msgSocket = socket_accept($socket)) == false){
echo "錯誤(socket_accept):".socket_strerror(socket_last_error())."<br />";
break;
}
/*
this function will accept incoming connections on that socket.
Once a successful connection is made, a new socket resource is returned, which may be used for communication.
If there are multiple connections queued on the socket, the first will be used.
If there are no pending connections, socket_accept() will block until a connection becomes present.
If socket has been made non-blocking using socket_set_blocking() or socket_set_nonblock(), FALSE will be returned.
*/
$msg = "Welcome!<br />";
//socket_write($msg,$msg,strlen($msg));
$command = "";
while(true){
if(($buf = socket_read($msgSocket,2048,PHP_BINARY_READ)) == false){
echo "錯誤(socket_read):".socket_strerror(socket_last_error())."<br />";
break 2;
}
/*
The function socket_read() reads from the socket resource socket created by the socket_create() or socket_accept() functions.
The maximum number of bytes read is specified by the length parameter.
Otherwise you can use \r, \n, or \0 to end reading (depending on the type parameter, see below).
*/
/*
if(!$buf = trim($buf))
continue; // ????
if($buf == "quit")
break;
if($buf == "shutdown"){
socket_close($msgSocket);
break 2;
}
$tallBack = "You say:$buf\n";
socket_write($msgSocket,$tallBack,strlen($tallBack));
*/
if(ord($buf) != 13)
$command .= $buf;
else{
$command1 = "You Say:$command\r\n";
socket_write($msgSocket,$command1,strlen($command1));
echo "User typed:".$command."<br />";
$command = "";
}
}
socket_close($msgSocket);
}
socket_close($socket);
?>
然后打開CMD,輸入:telnet 192.40.7.93 10000,自己體驗去吧!

注,要把:php_sockets.dll 打開
您可能感興趣的文章:
- PHP程序員簡單的開展服務(wù)治理架構(gòu)操作詳解(一)
- php獲取服務(wù)器端mac和客戶端mac的地址支持WIN/LINUX
- PHP 服務(wù)器配置(使用Apache及IIS兩種方法)
- PHP 顯示客戶端IP與服務(wù)器IP的代碼
- php獲取服務(wù)器信息的實現(xiàn)代碼
- php socket客戶端及服務(wù)器端應(yīng)用實例
- PHP向socket服務(wù)器收發(fā)數(shù)據(jù)的方法
- 利用php獲取服務(wù)器時間的實現(xiàn)代碼
- PHP實現(xiàn)服務(wù)器狀態(tài)監(jiān)控的方法
- PHP程序員簡單的開展服務(wù)治理架構(gòu)操作詳解(二)
相關(guān)文章
PHP XML error parsing SOAP payload on line 1
PHP中GBK頁面調(diào)用WebService的編碼問題:XML error parsing SOAP payload on line 12010-06-06php代碼中使用換行及(\n或\r\n和br)的應(yīng)用
瀏覽器識別不了\n或\r\n,這兩個換行符是文本換行符,文本文件有效;如果需要將結(jié)果輸出到瀏覽器或打印到顯示器,代碼中使用br;如果只是在源代碼中換行,則使用\n或\r\n,感興趣的朋友可以了解下,或許對你學(xué)習(xí)php有所幫助2013-02-02PHP正則表達(dá)式函數(shù)preg_replace用法實例分析
這篇文章主要介紹了PHP正則表達(dá)式函數(shù)preg_replace用法,結(jié)合實例形式分析了PHP正則表達(dá)式函數(shù)preg_replace基本功能、參數(shù)描述與相關(guān)使用技巧,需要的朋友可以參考下2020-06-06php進程(線程)通信基礎(chǔ)之System V共享內(nèi)存簡單實例分析
這篇文章主要介紹了php進程(線程)通信基礎(chǔ)之System V共享內(nèi)存,結(jié)合簡單實例形式分析了PHP System V共享內(nèi)存原理、相關(guān)函數(shù)與基本使用技巧,需要的朋友可以參考下2019-11-11