dtbi27903
2015-06-15 11:59
浏览 61
已采纳

在Windows中的cmd中运行服务器

I created a simple client server codes in PHP and I run it through Wamp server localhost in browser. It works but when I run it in cmd, the output looks like this :

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`

Why does this happen? Do I need to edit any settings in windows? It seems to work through browser but I need to run server through cmd to see stats. My server code is as follow :

<?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."
";
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dosc9472 2015-06-15 12:21
    已采纳

    You'll need to install (or enable) the Socket PHP extension: http://www.php.net/manual/en/sockets.installation.php

    You can enable the sockets extension on wamp, Wamp Icon -> PHP -> PHP Extensions -> Check php_sockets .

    If its already checked ,please inform me.

    Second way, call php in Administrator privilege CMD command line.So please open your cmd in 'Run In Administrator' mode.

    And please change your php_sockets extension manually in c:\wamp\bin\php\php5.x.y\php.ini. Basicly remove ';' beginning of the ';php_sockets.dll' line. If not exist add the line end of the extensions php_sockets.dll. Beacuse the cli-php call different php.ini file.

    点赞 打赏 评论

相关推荐 更多相似问题