PHP實(shí)現(xiàn)Socket服務(wù)器的代碼
更新時(shí)間:2008年04月03日 19:08:47 作者:
PHP實(shí)現(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 "錯(cuò)誤(socket_create):".socket_strerror(socket_last_error())."<br />";
if(socket_bind($socket,$address,$port) == false)
echo "錯(cuò)誤(socket_bind):".socket_strerror(socket_last_error())."<br />";
if(socket_listen($socket) == false)
echo "錯(cuò)誤(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 "錯(cuò)誤(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 "錯(cuò)誤(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);
?>
然后打開(kāi)CMD,輸入:telnet 192.40.7.93 10000,自己體驗(yàn)去吧!

注,要把:php_sockets.dll 打開(kāi)
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 "錯(cuò)誤(socket_create):".socket_strerror(socket_last_error())."<br />";
if(socket_bind($socket,$address,$port) == false)
echo "錯(cuò)誤(socket_bind):".socket_strerror(socket_last_error())."<br />";
if(socket_listen($socket) == false)
echo "錯(cuò)誤(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 "錯(cuò)誤(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 "錯(cuò)誤(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);
?>
然后打開(kāi)CMD,輸入:telnet 192.40.7.93 10000,自己體驗(yàn)去吧!

注,要把:php_sockets.dll 打開(kāi)
您可能感興趣的文章:
- PHP程序員簡(jiǎn)單的開(kāi)展服務(wù)治理架構(gòu)操作詳解(一)
- php獲取服務(wù)器端mac和客戶端mac的地址支持WIN/LINUX
- PHP 服務(wù)器配置(使用Apache及IIS兩種方法)
- PHP 顯示客戶端IP與服務(wù)器IP的代碼
- php獲取服務(wù)器信息的實(shí)現(xiàn)代碼
- php socket客戶端及服務(wù)器端應(yīng)用實(shí)例
- PHP向socket服務(wù)器收發(fā)數(shù)據(jù)的方法
- 利用php獲取服務(wù)器時(shí)間的實(shí)現(xiàn)代碼
- PHP實(shí)現(xiàn)服務(wù)器狀態(tài)監(jiān)控的方法
- PHP程序員簡(jiǎn)單的開(kāi)展服務(wù)治理架構(gòu)操作詳解(二)
相關(guān)文章
php中根據(jù)某年第幾天計(jì)算出日期年月日的代碼
在PHP中,使用內(nèi)置的date()函數(shù)很容易得到任意一天是當(dāng)前年的第幾天,格式為date('z'),為此,很多PHP程序會(huì)用一年中的第幾天作為數(shù)據(jù)庫(kù)的索引(index)。2011-02-02
30 個(gè)很棒的PHP開(kāi)源CMS內(nèi)容管理系統(tǒng)小結(jié)
本文匯集了30個(gè)優(yōu)秀的開(kāi)源CMS建站系統(tǒng),采用PHP開(kāi)發(fā)。以下列表不分先后順序2011-10-10
php+mysqli批量查詢多張表數(shù)據(jù)的方法
這篇文章主要介紹了php+mysqli批量查詢多張表數(shù)據(jù)的方法,涉及multi_query、store_result及more_results等函數(shù)的使用技巧,需要的朋友可以參考下2015-01-01
淺析php-fpm靜態(tài)和動(dòng)態(tài)執(zhí)行方式的比較
這篇文章主要介紹了php-fpm靜態(tài)和動(dòng)態(tài)執(zhí)行方式的比較,較為詳細(xì)的分析了php-fpm靜態(tài)和動(dòng)態(tài)執(zhí)行方式的原理、參數(shù)功能與相關(guān)使用技巧,需要的朋友可以參考下2016-11-11
PHP實(shí)現(xiàn)實(shí)時(shí)生成并下載超大數(shù)據(jù)量的EXCEL文件詳解
EXCEL文件的處理是我們?cè)谌粘9ぷ髦薪?jīng)常會(huì)遇到的,這篇文章主要給大家介紹了關(guān)于利用PHP如何實(shí)現(xiàn)實(shí)時(shí)生成并下載超大數(shù)據(jù)量的EXCEL文件,通過(guò)文中介紹的這個(gè)方法對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-10-10

