gpt4 book ai didi

php - 错误 :Could not bind to socket in php programming

转载 作者:行者123 更新时间:2023-12-04 06:03:27 24 4
gpt4 key购买 nike

这是一个示例,显示 TCP 服务器接受字符串作为输入,将其反转并将其返回给客户端。
这是代码:

<?php
$host = "127.0.0.1";
$port = 1234;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
$input = socket_read($spawn, 1024) or die("Could not read input\n");
$input = trim($input);
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
socket_close($spawn);
socket_close($socket);
?>

并得到这样的错误:
  Warning: socket_bind() [function.socket-bind]: unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in C:\xampp\htdocs\socket.php on line 5
Could not bind to socket

我能做些什么来解决这个问题?

最佳答案

绑定(bind)前将此选项添加到程序中

   if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($socket));
exit;
}

供引用 http://www.php.net/manual/en/function.socket-bind.php

您也可以查看 http://www.php.net/manual/en/function.socket-set-option.php详情

关于php - 错误 :Could not bind to socket in php programming,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8667330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com