首页 > 后端开发 > php教程 > PHP Socket实例

PHP Socket实例

WBOY
发布: 2016-06-23 14:35:00
原创
988 人浏览过

简介:这是PHP Socket实例的详细页面,介绍了和php,php, socket, code PHP Socket实例有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=345994' scrolling='no'> 下面是一段 php socket编程的例子,当然也可以使用fsockopen
<?phperror_reporting (E_ALL);echo "<h2>TCP/IP Connection\n";/* Get the port for the WWW service. */$service_port = getservbyname('www', 'tcp');/* Get the IP address for the target host. */$address = gethostbyname('www.example.com');/* Create a TCP/IP socket. */$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);if ($socket === false) {    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";} else {    echo "OK.\n";}echo "Attempting to connect to '$address' on port '$service_port'...";$result = socket_connect($socket, $address, $service_port);if ($result === false) {    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";} else {    echo "OK.\n";}$in = "HEAD / HTTP/1.1\r\n";$in .= "Host: www.example.com\r\n";$in .= "Connection: Close\r\n\r\n";$out = '';echo "Sending HTTP HEAD request...";socket_write($socket, $in, strlen($in));echo "OK.\n";echo "Reading response:\n\n";$buf = 'This is my buffer.';if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {    echo "Read $bytes bytes from socket_recv(). Closing socket...";} else {    echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";}socket_close($socket);echo $buf . "\n";echo "OK.\n\n";?> 
登录后复制

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/345994.html pageNo:4
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板