注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

phperwuhan的博客

记载一个phper的历程!phperwuhan.blog.163.com

 
 
 

日志

 
 

php下的socket客户端与服务器端  

2010-02-21 14:39:53|  分类: php |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

来源:http://www.yaiba.net/php-socket-client-and-server-side-184/

php下的socket技术。

要编译本扩展模块不需要外部库文件。

The socket functions described here are part of an extension to PHP

which must be enabled at compile time by giving the –enable-sockets option to configure.

注: IPv6 支持是 PHP 5.0.0 新加的。

例子 1. Socket example: Simple TCP/IP server

This example shows a simple talkback server. Change the address and port variables to suit your setup and execute. You may then connect to the server with a command similar to: telnet 192.168.1.53 10000 (where the address and port match your setup). Anything you type will then be output on the server side, and echoed back to you. To disconnect, enter 'quit'.

#!/usr/local/bin/php -q

< ?php

error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */

set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting

* as it comes in. */

ob_implicit_flush();

$address = '192.168.1.53';

$port = 10000;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {

echo “socket_create() failed: reason: ” . socket_strerror($sock) . “\n”;

}

if (($ret = socket_bind($sock, $address, $port)) < 0) {

echo “socket_bind() failed: reason: ” . socket_strerror($ret) . “\n”;

}

if (($ret = socket_listen($sock, 5)) < 0) {

echo “socket_listen() failed: reason: ” . socket_strerror($ret) . “\n”;

}

do {

if (($msgsock = socket_accept($sock)) < 0) {

echo “socket_accept() failed: reason: ” . socket_strerror($msgsock) . “\n”;

break;

}

/* Send instructions. */

$msg = “\nWelcome to the PHP Test Server. \n” .

“To quit, type 'quit'. To shut down the server type 'shutdown'.\n”;

socket_write($msgsock, $msg, strlen($msg));

do {

if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {

echo “socket_read() failed: reason: ” . socket_strerror($ret) . “\n”;

break 2;

}

if (!$buf = trim($buf)) {

continue;

}

if ($buf == 'quit') {

break;

}

if ($buf == 'shutdown') {

socket_close($msgsock);

break 2;

}

$talkback = “PHP: You said '$buf'.\n”;

socket_write($msgsock, $talkback, strlen($talkback));

echo “$buf\n”;

} while (true);

socket_close($msgsock);

} while (true);

socket_close($sock);

?>

例子 2. Socket example: Simple TCP/IP client

This example shows a simple, one-shot HTTP client. It simply connects to a page, submits a HEAD request, echoes the reply, and exits.

< ?php

error_reporting(E_ALL);

echo “

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 < 0) {

echo “socket_create() failed: reason: ” . socket_strerror($socket) . “\n”;

} else {

echo “OK.\n”;

}

echo “Attempting to connect to '$address' on port '$service_port'…”;

$result = socket_connect($socket, $address, $service_port);

if ($result < 0) {

echo “socket_connect() failed.\nReason: ($result) ” . socket_strerror($result) . “\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”;

while ($out = socket_read($socket, 2048)) {

echo $out;

}

echo “Closing socket…”;

socket_close($socket);

echo “OK.\n\n”;

?>

  评论这张
 
阅读(443)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017