【问题标题】:PHP Sockets Errors (connection refused and No such file or directory)PHP 套接字错误(连接被拒绝并且没有这样的文件或目录)
【发布时间】:2011-02-16 07:02:31
【问题描述】:

我正在编写一个服务器应用程序(广播)和一个客户端(中继器)。多个中继器可以同时连接到广播器,发送信息,广播器会将消息重定向到匹配的中继器(例如中继器1发送到广播器,广播器发送到中继器43,中继器2->广播器->中继器73...)

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

中继器和广播器都位于同一台服务器上,因此我使用的是 AF_UNIX 套接字,但两个文件位于不同的文件夹中。

我已经为relayer尝试了两种方法,但都失败了,第一种是使用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,但都没有成功。

对此有什么想法吗?

【问题讨论】:

  • 我确信所有这些都可以浓缩为一两行。
  • 也许,你能给我提供那些工作线吗?如您所见,我确实尝试过但失败了

标签: php sockets


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-06-05
    • 2018-09-30
    • 2015-11-02
    • 2020-09-12
    • 2013-05-12
    • 2019-12-22
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多