php - 无法绑定(bind)地址[0] : php error

标签 php sockets client wampserver

无法绑定(bind)地址 [0]:每个套接字地址(协议(protocol)/网络地址/端口)通常只允许使用一次....
错误是由我的 php 服务器页面给出的。我尝试了不同的端口号,就像从 cmd 中查看 netstat -an 一样。我也在谷歌上搜索但没有解决方案。我正在使用 wamp 服务器并在本地工作。
谢谢 。

<?php
// don't timeout
//echo phpinfo();
set_time_limit (0);
// set some variables
$host = "127.0.0.1";
$port = 1234;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
echo "Waiting for connections...\n";
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
echo "Received connection request\n";
// write a welcome message to the client
$welcome = "Roll up, roll up, to the greatest show on earth!\n? ";
socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send connect string\n");
// keep looping and looking for client input
do
{
  // read client input
  $input = socket_read($spawn, 1024, 1) or die("Could not read input\n");
  if (trim($input) != "")
  {
    echo "Received input: $input\n";
    // if client requests session end
    if (trim($input) == "END")
    {
      // close the child socket
      // break out of loop
      socket_close($spawn);
      break;
    }
    // otherwise...
    else
    {
      // reverse client input and send back
      $output = strrev($input) . "\n";
      socket_write($spawn, $output . "? ", strlen (($output)+2)) or die("Could not write output\n");
      echo "Sent output: " . trim($output) . "\n";
    }
  }
} while (true);
// close primary socket
socket_close($socket);
echo "Socket terminated\n";
?>

最佳答案

呃……这是在网页上运行的吗?如果是这样,对页面的每次点击都会导致脚本尝试绑定(bind)到端口 1234,这不会一次只发生一个。其他人都会死。

如果不是,那么我可以立即想到绑定(bind)失败的两个原因:另一个程序已经在使用该端口,或者防火墙阻止了它。后者不应该是 127.0.0.1 的情况,但我见过更奇怪的事情发生。

关于php - 无法绑定(bind)地址[0] : php error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3377927/

相关文章:

c - C 中两个服务器的套接字客户端

php上传数据到mysql服务器

Python:检测套接字何时因任何原因断开连接?

php - 获取表中重复数字值最多的名称

windows - 与 Windows 上本地主机 IPC 的命名管道相比,TCP 套接字有多慢?

c - inet_pton() 给出 "Segmentation fault"

http - 用于 Rust 的同步 http 客户端?

c - 在 rabbitmq-c 中使用默认交换

javascript - Laravel 两次运行 axios 发送的请求

php - 解析错误(T_VARIABLE)