我在PHP中创建了一个简单的客户端服务器代码,我在浏览器中通过Wamp服务器localhost运行它。它可以工作但是当我在cmd中运行时,输出看起来像这样:
c:\wamp\www\Converter>php testserver.php
PHP Fatal error: Call to undefined function socket_create() in C:\wamp\www\Converter\testserver.php on line 15
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Converter\testserver.php:0
Fatal error: Call to undefined function socket_create() in C:\wamp\www\Converter\testserver.php on line 15
Call Stack:
0.0005 248032 1. {main}() C:\wamp\www\Converter\testserver.php:0
c:\wamp\www\Converter>php ex3_server.php
PHP Fatal error: Call to undefined function socket_create() in C:\wamp\www\Converter\ex3_server.php on line 5
PHP Stack trace:
PHP 1. {main}() C:\wamp\www\Converter\ex3_server.php:0
Fatal error: Call to undefined function socket_create() in C:\wamp\www\Converter\ex3_server.php on line 5
Call Stack:
0.0002 232968 1. {main}() C:\wamp\www\Converter\ex3_server.php:0`
为什么会这样?我是否需要编辑Windows中的任何设置?它似乎通过浏览器工作,但我需要通过cmd运行服务器来查看统计信息。 我的服务器代码如下:
<?php
//server_side
$address="localhost";//your ip
$port="10012";//port number
$socket=socket_create(AF_INET,SOCK_STREAM,0);
if($socket)
echo "<br>Successfully socket created<br>";
else
echo "<br>Cannot create socket<br>";
$bind=socket_bind($socket,$address,$port);
if($bind)
echo "<br>Successfully binded to socket<br>";
else
echo "<br>Cannot bind socket<br>";
//listens for a connection on a socket
$listen=socket_listen($socket) or die ("could not setup");
if($listen)
echo "<br>Successfully listen connection<br>";
else
echo "<br>Cannot listen connection<br>";
//accepts a connection on a socket
$accept=socket_accept($socket) or die ("could not accept");
if($accept)
echo "<br>Successfully accepted request<br>";
else
echo "<br>Connot accept request<br>";
//send message 1
$message1="Hello Client";
$write=socket_write($accept,$message1);
//read reply2
$read2=trim(socket_read($socket,1024));
echo $read2."\n";
?>
答案 0 :(得分:1)
您需要安装(或启用)Socket PHP扩展程序:http://www.php.net/manual/en/sockets.installation.php
您可以在wamp,Wamp Icon - &gt;上启用套接字扩展。 PHP - &gt; PHP扩展 - &gt;检查php_sockets。
如果已经检查过,请通知我。
第二种方法,在管理员权限CMD命令行中调用php。所以请在&#39; Run In Administrator&#39;中打开你的cmd。模式。
请在c:\wamp\bin\php\php5.x.y\php.ini
中手动更改您的php_sockets扩展程序。基本上删除&#39;;&#39; &#39; ;php_sockets.dll
&#39;的开始线。如果不存在,请添加扩展名php_sockets.dll
的行尾。因为cli-php调用了不同的php.ini
文件。