如何在PHP中连接不同端口的套接字?
这段代码运行良好。 我在系统中的端口: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 以外的端口,请问有什么想法吗? 谢谢
This code is working fine.
I am successfully tested socket connection in my system in port:80 but not able to connect in different port .
<?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";
?>
Output:
Attempting to connect to '127.0.0.1' on port '8000'...socket_connect() failed.
Reason: () Connection refused
Closing socket...OK.
I need to connect in port different than 80, so please any Idea?
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有打开服务器套接字,则无法连接到通用端口。
You can't connect to a generic port if there isn't a server socket opened there.