PHP实现Socket服务器的代码
2015-01-24信息快讯网
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,自己体验去吧!
510){this.resized=true;this.style.width=510;}">
注,要把:php_sockets.dll 打开
php checkdate、getdate等日期时间函数操作详解
PHP set_time_limit(0)长连接的实现分析
php 用checkbox一次性删除多条记录的方法
GBK的页面输出JSON格式的php函数
php+mysql事务rollback&commit示例
Asp.net 文本框全选的实现
PHP MYSQL乱码问题,使用SET NAMES utf8校正
phplock(php进程锁) v1.0 beta1
PHP CKEditor 上传图片实现代码
php win下Socket方式发邮件类
php natsort内核函数浅析第1/2页
php array_map array_multisort 高效处理多维数组排序
mysql_fetch_row,mysql_fetch_array,mysql_fetch_assoc的区别
使用PHP socke 向指定页面提交数据
php socket方式提交的post详解
php读取30天之内的根据算法排序的代码
mysql+php分页类(已测)
FCKeditor添加自定义按钮
在PHP中使用Sockets 从Usenet中获取文件
set_include_path在win和linux下的区别
Http 1.1 Etag 与 Last-Modified提高php效率
使用ETags减少Web应用带宽和负载第1/2页
EPSON打印机 连供墨水系统 维修有哪些保养窍门第1/2页
wordpress之wp-settings.php
php 用sock技术发送邮件的函数
dedecms防止FCK乱格式化你的代码的修改方法
一个模仿oso的php论坛程序源码(之三)第1/2页
一个模仿oso的php论坛程序源码(之二)第1/3页
Smarty+QUICKFORM小小演示
发布一个用PHP fsockopen写的HTTP下载的类
模拟flock实现文件锁定