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

在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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘