PHP实现Socket服务器的代码

PHP中文网
发布: 2016-07-29 08:37:48
原创
1021人浏览过

<?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,自己体验去吧! 

20160614222731_1466.gif

注,要把:php_sockets.dll 打开 

代码小浣熊
代码小浣熊

代码小浣熊是基于商汤大语言模型的软件智能研发助手,覆盖软件需求分析、架构设计、代码编写、软件测试等环节

代码小浣熊 51
查看详情 代码小浣熊

以上就介绍了 PHP实现Socket服务器的代码,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号