PHP Socket 编程

yipeiwu_com5年前PHP代码库
下面是相应的代码:
PHP 代码:
复制代码 代码如下:

<?
// 设置一些基本的变量
$host = "192.168.1.99";
$port = 1234;
// 设置超时时间
set_time_limit(0);
// 创建一个Socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
socket\n");
//绑定Socket到端口
$result = socket_bind($socket, $host, $port) or die("Could not bind to
socket\n");
// 开始监听链接
$result = socket_listen($socket, 3) or die("Could not set up socket
listener\n");
// accept incoming connections
// 另一个Socket来处理通信
$spawn = socket_accept($socket) or die("Could not accept incoming
connection\n");
// 获得客户端的输入
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// 清空输入字符串
$input = trim($input);
//处理客户端输入并返回结果
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write
output\n");
// 关闭sockets
socket_close($spawn);
socket_close($socket);
?>

相关文章

PHP实现在windows下配置sendmail并通过mail()函数发送邮件的方法

PHP实现在windows下配置sendmail并通过mail()函数发送邮件的方法

本文实例讲述了PHP实现在windows下配置sendmail并通过mail()函数发送邮件的方法。分享给大家供大家参考,具体如下: 1、php mail()函数在windows不能用,...

php常用ODBC函数集(详细)

ODBC连接类函数odbc_connect函数:打开一个ODBC连接odbc_close函数:关闭一个已经打开的ODBC连接odbc_close_all函数:关闭所有已经打开的ODBC连...

php 字符串压缩方法比较示例

php 提供的字符串压缩方法有 1.gzcompress — Compress a string This function compress the given string usin...

php反射学习之依赖注入示例

本文实例讲述了php反射学习之依赖注入。分享给大家供大家参考,具体如下: 先看代码: <?php if (PHP_SAPI != 'cli') { exit('Pl...

PHP多进程编程之僵尸进程问题的理解

PHP多进程编程之僵尸进程问题的理解 使用pcntl_fork函数可以让PHP实现多进程并发或者异步处理的效果:https://www.jb51.net/article/125789.h...