簡體   English   中英

socket_connect(): 無法連接 [10061] - 目標機器主動拒絕它

[英]socket_connect(): unable to connect [10061] - target machine actively refused it

我正在嘗試使用 PHP socket_connect() 函數連接用 C 編寫的目標,但出現以下錯誤:

警告:socket_connect():無法連接 [10061]:無法建立連接,因為目標機器主動拒絕它。

這是我正在運行的代碼:

    $service_port = 1234; 
    $address = "xx.xxx.xx.xxx";
    $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<br>";
    }       
    echo "Attempting to connect to '$address' on port '$service_port'...\n";        

    $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<br>";
    }

我在 Windows 7 中使用帶有 PHP 5.4 的 xampp 服務器,並且我已禁用防火牆設置。 我仍然收到相同的拒絕錯誤。

請幫我解決這個問題。 [我在過去 3 天一直在與這個問題作斗爭]

提前致謝。

希望下面的鏈接有幫助。 結帳解決方案。 http://www.codeproject.com/Questions/426966/System-Net-Sockets-SocketExcep

如果你在同一台機器上運行客戶端和服務器代碼,那么你必須像下面這個例子一樣混合它們:

<?php

//client

$host    = "localhost";
$port    = 80;
$message = "Hello Server";
echo "Message To server :".$message;
// create socket
$socket1 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// connect to server
$result1 = socket_connect($socket1, $host, $port) or die("Could not connect to server\n");  


//server


// don't timeout!
set_time_limit(100);
// create socket
$socket2 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
// bind socket to port
$result2 = socket_bind($socket2, $host, $port) or die("Could not bind to socket\n");
// start listening for connections
$result2 = socket_listen($socket2, 3) or die("Could not set up socket listener\n");

// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket2) or die("Could not accept incoming connection\n");



// send string to server
socket_write($socket1, $message, strlen($message)) or die("Could not send data to server\n");


// read client input
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "Client Message : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");


$result1 = socket_read ($socket1, 1024) or die("Could not read server response\n");
echo "Reply From Server  :".$result1;

socket_close($spawn);
socket_close($socket2);
socket_close($socket1);
?>

我意識到這是一個舊帖子,但是,我最近在使用 IIS 7 下運行的 php 時遇到了這個問題,並且在多次谷歌搜索后無法找到解決方案。

許多站點建議通過更改 php.ini 文件(然后停止並重新啟動 IIS)來啟用套接字。

我發現我必須在 IIS 7 管理器中啟用套接字才能使套接字工作。

我發現只是手動更改 php.ini 文件並沒有在 php 中啟用套接字(以及 IIS 7)。 我下面的示例代碼顯示 phpinfo(); 如果啟用了套接字,則會有一個名為 Sockets 的特定部分,它會顯示自己已啟用。

IIS 管理器 7 --> 向下滾動到 PHP 管理器並選擇 --> 向下滾動到 PHP 擴展並選擇啟用/禁用擴展的選項 --> 如果尚未啟用,則啟用套接字 --> 重新啟動 IIS。

我正在運行桌面套接字服務器(我編寫的 - 使用端口 9999)並且能夠使用我編寫的桌面客戶端連接到我的服務器(用於測試,在同一台 Windows 7 PC 上運行),但無法使用運行的 php 進行連接同一台電腦。

我用於測試連接的 php 代碼如下所示:

<?php
echo "hello<br><br>";
echo phpinfo();
$fp = stream_socket_client("tcp://127.0.0.1:9999", $errno, $errstr, 30);
if (!$fp) {
   echo "error<br><br>";
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "GET / HTTP/1.0\r\nHost: www.google.com\r\nAccept: */*\r\n\r\n"); //just returns an acknowledgement
    while (!feof($fp)) {
        echo fgets($fp, 1024);
    }
    fclose($fp);
}
?>

我希望這可以幫助別人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM