PHP套接字错误(连接被拒绝,没有这样的文件或目录)

时间:2010-05-06 08:08:15

标签: php sockets

我正在写一个服务器应用程序(广播公司)和一个客户端(中继者)。多个中继器可以同时连接到广播公司,发送信息并且广播公司将消息重定向到匹配的中继器(例如,relayer1发送给发送到relayer43的广播公司,relayer2 - >广播公司 - > relayer73 ......)

服务器部分正在运行,因为我已经使用telnet客户端对其进行了测试,尽管此时它只有一个echo服务器。

中继和广播公司都坐在同一台服务器上,所以我使用的是AF_UNIX套接字,但这两个文件都在不同的文件夹中。

我尝试了两种方法,但都失败了,第一种方法是使用socket_create:

public function __construct()
{  
    // where is the socket server?
    $this->_sHost = 'tcp://127.0.0.1';
    $this->_iPort = 11225;

    // open a client connection
    $this->_hSocket = socket_create(AF_UNIX, SOCK_STREAM, 0);

    echo 'Attempting to connect to '.$this->_sHost.' on port '.$this->_iPort .'...';
    $result = socket_connect($this->_hSocket, $this->_sHost, $this->_iPort);
    if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($this->_hSocket)) . "\n";
    } else {
    echo "OK.\n";

}

返回“警告:socket_connect():无法连接[2]:在第27行的relayer.class.php中没有这样的文件或目录”和(它从命令行运行)它通常也会返回分段错误。

第二种方法是使用pfsockopen:

public function __construct()
{  
// where is the socket server?
$this->_sHost = 'tcp://127.0.0.1';
$this->_iPort = 11225;

// open a client connection  
$fp = pfsockopen ($this->_sHost, $this->_iPort, $errno, $errstr);
if (!$fp)
{
  $result = "Error: could not open socket connection";
}
else
{
  // get the welcome message
  fgets ($fp, 1024);
  // write the user string to the socket
  fputs ($fp, 'Message ' . __LINE__);
  // get the result
  $result .= fgets ($fp, 1024);
  // close the connection
  fputs ($fp, "END");
  fclose ($fp);
  // trim the result and remove the starting ?
  $result = trim($result);
  $result = substr($result, 2);
  // now print it to the browser
}

只返回错误“警告:pfsockopen():无法连接到第33行的relayer.class.php中的tcp://127.0.0.1:11225(拒绝连接)”

在所有测试中,我尝试使用不同的主机名127.0.0.1,localhost,tcp://127.0.0.1,192.168.0.199,tcp://192.168.0.199,但没有一个有效。

有关于此的任何想法吗?

1 个答案:

答案 0 :(得分:2)

显然其中一个防火墙在这里发挥作用,抱歉浪费了你的时间。如果有人遇到这个问题,请检查您的端口是否打开,它为我做了诀窍