php服务器代码,PHP实现Socket服务器的代码

PHP实现Socket服务器的代码

更新时间:2008年04月03日 19:08:47   作者:

PHP实现Socket服务器的代码

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())."
";

/*

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())."
";

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!
";

//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())."
";

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."
";

$command = "";

}

}

socket_close($msgSocket);

}

socket_close($socket);

?>

然后打开CMD,输入:telnet 192.40.7.93 10000,自己体验去吧!

20080403190845579.gif

注,要把:php_sockets.dll 打开

相关文章

1a1b05c64693fbf380aa1344a7812747.png

PHPMailer 中文使用说明小结,对于操作phpmailer的朋友作为参考资料。2010-01-01

4f55910a645b073bc4fc65dc10dc14bd.png

这篇文章主要介绍了php解析字符串里所有URL地址的方法,涉及php操作数组、字符串及URL的技巧,具有一定参考借鉴价值,需要的朋友可以参考下2015-04-04

0ea3c7666119d5615e582f823fb3fad6.png

这篇文章主要介绍了php简单实现sql防注入的方法,涉及addslashes函数的使用及正则过滤的相关技巧,非常简单实用,需要的朋友可以参考下2016-04-04

4f96a78db829b1556ff16de21e013c7a.png

如何用 PHP 来获取客户端 IP 和 服务器(主机)的 IP 呢?需要的朋友可以参考下。2010-10-10

8cc1031babc6aff2319f1c6af8544aa0.png

这篇文章主要介绍了PHP使用http_build_query()构造URL字符串的方法,结合实例形式较为详细的分析了http_build_query函数的功能,使用技巧与相关注意事项,需要的朋友可以参考下2016-04-04

0c932a99bb7b6f23c937db507070cc7b.png

这篇文章主要介绍了PHP 文件写入和读取操作,结合实例形式详细分析了PHP针对文件的打开、读取、写入及指针操作相关使用技巧,需要的朋友可以参考下2019-11-11

cca732bf65a93ed2ec0ac80c638460fe.png

简单地写了一个PHP的图片上传,可以支持jpg、png、gif等格式的图片,如果你想支持更多的格式,比如bmp,可以自己扩充。不是面向对象的,在这里简单记录下,以便以后取用2011-11-11

2d9f31f2af7b675a3d153d2b7f1035a7.png

本篇文章是对PHP实现多进程并行执行脚本进行了详细的分析介绍,需要的朋友参考下2013-06-06

b452cee8ec5cd9e58ab98eba17281e59.png

在登录的时候记住用户输入的密码在某些情况下是很有必要的,下面是一个小例子,感兴趣的朋友可以参考下哈,希望对你有所帮助2013-05-05

f4838ec7e2d4da28e0b57d4e852dadd4.png

这篇文章主要介绍了php中 ob_start等函数截取标准输出的方法的相关资料,需要的朋友可以参考下2015-06-06

最新评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值