- 论坛徽章:
- 0
|
在phpinfo()函数中看到socket一段如下显示:
Sockets Support enabled
然后,试http://www.zend.com/pecl/tutorials/sockets.php 中的例程
- #!/usr/local/bin/php –q
- <?php
- // Set time limit to indefinite execution
- set_time_limit (0);
- // Set the ip and port we will listen on
- $address = 'localhost';
- $port = 9000;
- // Create a TCP Stream socket
- $sock = socket_create(AF_INET, SOCK_STREAM, 0);
- // Bind the socket to an address/port
- socket_bind($sock, $address, $port) or die('Could not bind to address');
- // Start listening for connections
- socket_listen($sock);
- /* Accept incoming requests and handle them as child processes */
- $client = socket_accept($sock);
- // Read the input from the client – 1024 bytes
- $input = socket_read($client, 1024);
- // Strip all white spaces from input
- $output = ereg_replace("[ \t\n\r]","",$input).chr(0);
- // Display output back to client
- socket_write($client, $output);
- // Close the client (child) socket
- socket_close($client);
- // Close the master sockets
- socket_close($sock);
- ?>
复制代码
按照In order to run the program, make sure that the first line #!/usr/local/bin/php –q is the location of the PHP CLI (or CGI) binary. You will need to change the mode so it is executable (chmod 755 socket_server.php – where socket_server.php is the name of the file) and run it using ./socket_server.php from the command line.
来执行,竟然出错, 请见出错提示图
相反地,代码在同一服务器上,而用浏览器运行一段代码如下(从PHP manual中摘下)
- <?php
- error_reporting(E_ALL);
- echo "<h2>TCP/IP Connection</h2>\n";
- /* Get the port for the WWW service. */
- //$service_port = getservbyname('www', 'tcp');
- $service_port = 56753;
- /* Get the IP address for the target host. */
- //$address = gethostbyname('www.example.com');
- $address = "192.9.200.44" ;
- /* 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";
- ?>
复制代码
却是安然无恙,如下安然无恙图,phpinfo()中的Sockets Support = enable是何意思?
何故致此? 还望高人指点迷津!
[ 本帖最后由 alinker 于 2006-4-25 09:40 编辑 ] |
-
12.gif
(12.52 KB, 下载次数: 29)
出错提示
-
13.gif
(9.73 KB, 下载次数: 26)
安然无恙
|