PHP   发布时间:2019-11-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP实现Socket服务器的代码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

<?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())."
"; if(sockeT_Bind($socket,$address,$port)==falsE)
echo"错误(sockeT_Bind):".socket_strerror(socket_last_error())."
"; if(socket_listen($socket)==falsE)
echo"错误(socket_listen):".socket_strerror(socket_last_error())."
"; /
Afterthesocketsockethasbeencreatedusingsocket_create()andboundtoanamewithsockeT_Bind(),
itmaybetoldtolistenforincomingconnectionsonsocket.
/ while(true){
if(($msgSocket=socket_accept($socket))==falsE){
echo"错误(socket_accept):".socket_strerror(socket_last_error())."
";
break;
} /
thisfunctionwillaccepTincomingconnectionsonthatsocket.
Onceasuccessfulconnectionismade,anewsocketresourceisreturned,whichmaybeusedforcommunication.
Iftherearemultipleconnectionsqueuedonthesocket,thefirstwillbeused.
Iftherearenopendingconnections,socket_accept()willblockuntilaconnectionbecomespresent.
Ifsockethasbeenmadenon-blockingusingsocket_seT_Blocking()orsocket_set_nonblock(),falSEwillbereturned.
/ $msg="Welcome!
";
//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())."
";
break2;
} /
Thefunctionsocket_read()readsfromthesocketresourcesocketcreatedbythesocket_create()orsocket_accept()functions.
ThemaximumnumberofbytesreadisspecifiedbytHelengthparameter.
Otherwiseyoucanuse\r,\n,or\0toendreading(dependingonthetypeparameter,seebelow).
/ /
if(!$buf=trim($buf))
conTinue;//???? if($buf=="quit")
break; if($buf=="shutdown"){
socket_close($msgSocket);
break2;
} $tallBACk="Yousay:$buf\n";
socket_write($msgSocket,$tallBACk,strlen($tallBACk));
/ if(ord($buf)!=13)
$command.=$buf;
else{
$command1="YouSay:$command\r\n";
socket_write($msgSocket,$command1,strlen($command1));
echo"Usertyped:".$command."
";
$command="";
}
}
socket_close($msgSocket);
} socket_close($socket);
?> 然后打开CMD,输入:telnet192.40.7.9310000,自己体验去吧!
<IMG style="cursOR: pointer" onclick=javascript:window.open(this.src); src="http:https://files.jb51.cc/upload/20080403190845579.gif" onload="javascript:if(this.width>510){this.resized=true;this.style.width=510;}">
注,要把:php_sockets.dll 打开

大佬总结

以上是大佬教程为你收集整理的PHP实现Socket服务器的代码全部内容,希望文章能够帮你解决PHP实现Socket服务器的代码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:服务器