此代码工作正常。 我在我的系统中成功测试了套接字连接端口:80但无法连接到不同的端口。
<?php
echo "<pre>";
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Get the port for the WWW service. */
$service_port = 8000;
/* Get the IP address for the target host. */
$address = gethostbyname('localhost');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";
?>
输出:
Attempting to connect to '127.0.0.1' on port '8000'...socket_connect() failed.
Reason: () Connection refused
Closing socket...OK.
我需要连接不同于80的端口,所以请问任何想法? 谢谢
答案 0 :(得分:0)
如果没有打开服务器套接字,则无法连接到通用端口。