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 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程