【发布时间】:2023-03-31 16:44:01
【问题描述】:
以下是我创建的用于侦听传入消息(XML 字符串)的 PHP 脚本。
该 PHP 脚本托管在我的本地家庭服务器上的 13330 端口上,所以我可以在那里侦听传入的请求,对吧?所以我创建了套接字并将其绑定到文件所在的地址。
我收到此错误:警告:socket_bind():无法绑定地址 [0]:每个套接字地址(协议/网络地址/端口)通常只允许使用一次。
如果有人能告诉我为什么我会看到这种情况,我将不胜感激。
谢谢
createSocketServer();
function createSocketServer() {
// Set time limit to indefinite execution
set_time_limit (0);
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 13330;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
echo '<p>Socket created</p>';
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
echo '<p>Socket binded</p>';
// Start listening for connections
socket_listen($sock);
echo '<p>Socket listening</p>';
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
// Read the input from the client – 1024 bytes
$input = socket_read($client, 1024);
// Strip all white spaces from input
$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
echo $output;
}
【问题讨论】:
-
很可能某些东西已经在使用该端口 - netstat 会显示什么?
-
@symcbean 我可以将套接字端口绑定到我托管 WAMP 服务器的同一个端口还是必须不同?
-
它必须不同:每个套接字地址(协议/网络地址/端口)通常只允许使用一次。
-
@symcbean 啊,我知道你在那里做了什么:P。稍后我会告诉你我如何使用不同的端口,谢谢。