PHP-socket_connect():无法连接[10061]:无法建立连接,因为目标计算机主动拒绝它

时间:2016-04-24 19:09:21

标签: php sockets client-server

我在php中运行客户端 - 服务器代码进行聊天。我收到此错误: socket_connect():无法连接[10061]:无法建立连接,因为目标计算机主动拒绝

我在这个问题上经历了很多问题和答案,但仍然无法解决这个问题。 这是我的代码:

for user-1:

  

$host   = "127.0.0.1";
$port_1 = 5001;
$port_2  = 50002;
// create socket
$socket_2 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$socket_1 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket_2, $host, $port_2) or die("Could not connect to server\n");  


socket_bind($socket_1, $host, $port_1) or die("Could not bind to socket\n");
socket_listen($socket_1, 10) or die("Could not set up socket listener\n");
$new_socket= socket_accept($socket_1) or die("Could not accept incoming connection\n");


        $result = socket_read ($socket_1, 1024);

    if (isset($_POST['submit']))
    {
        $message = $_POST['client_message'];
        socket_write($socket_2, $message, strlen($message)) or die("Could not send data to server\n");
    }
    // or die("Could not read server response\n");
    if(!empty($result))
    {
        echo "Reply From Server  :".$result;
    }
    socket_close($socket_1);
    socket_close($socket_2);
    socket_close($new_socket);
?
  

这是针对用户-2的

$host   = "127.0.0.1";
$port_1 = 5001;
$port_2  = 50002;
// create socket
$socket_2 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$socket_1 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket_2, $host, $port_2) or die("Could not connect to server\n");  


socket_bind($socket_1, $host, $port_1) or die("Could not bind to socket\n");
socket_listen($socket_1, 10) or die("Could not set up socket listener\n");
$new_socket= socket_accept($socket_1) or die("Could not accept incoming connection\n");


        $result = socket_read ($socket_1, 1024);

    if (isset($_POST['submit']))
    {
        $message = $_POST['client_message'];
        socket_write($socket_2, $message, strlen($message)) or die("Could not send data to server\n");
    }
    // or die("Could not read server response\n");
    if(!empty($result))
    {
        echo "Reply From Server  :".$result;
    }
    socket_close($socket_1);
    socket_close($socket_2);
    socket_close($new_socket);
?>

请帮我弄清楚错误。 谢谢

0 个答案:

没有答案