警告:socket_connect():无法连接[10060]

时间:2018-08-12 16:28:38

标签: php sockets

我正在这里的this套接字编程教程中。

但是当使用公共IP地址运行以下脚本时,我会收到此“警告”。

<?php 
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Couldn't create socket: [$errorcode] $errormsg \n");
}

echo "Socket created \n";
echo "<br/>";
//Connect socket to remote server
if(!socket_connect($sock , '8.8.8.8' , 80))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not connect: [$errorcode] $errormsg \n");
}

echo "Connection established \n";
echo "<br/>";

$message = "GET / HTTP/1.1\r\n\r\n";

//Send the message to the server
if( ! socket_send ( $sock , $message , strlen($message) , 0))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not send data: [$errorcode] $errormsg \n");
}

echo "Message send successfully \n";

但是,当我用自己的本地IP 127.0.0.1替换IP地址时,它可以工作。 否则,我会收到以下警告:

Socket created 

Warning: socket_connect(): unable to connect [10060]: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\test.php on line 13
Could not connect: [10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

为什么仅由于IP地址而收到此警告?

1 个答案:

答案 0 :(得分:1)

是的,这仅仅是因为IP地址。如果您仍然使用此地址测试套接字。您可以让服务器绑定到该地址以使套接字正常工作,也可以将IP更改为本地127.0.0.1