为什么php的fsockopen返回(资源暂时不可用)?

时间:2022-09-22 13:05:56

I have a very simple server php code like this

我有一个非常简单的服务器PHP代码,像这样

function listenForClients()
 {
      $this->serviceConnection = socket_create(AF_UNIX, SOCK_STREAM, 0);
      socket_bind($this->serviceConnection, "\tmp\mysock", 0);
      socket_listen($this->serviceConnection, 10000000);

      while($clientSocket = socket_accept($this->serviceConnection))
      {
           $clientMessage = socket_read($clientSocket, 1024);
           socket_close($clientSocket);
      }
 }


Then I have a very simple client that does this

然后我有一个非常简单的客户端来做到这一点

for ( $counter = 0; $counter <= 1000; $counter ++) {    
   $fp = fsockopen("unix///tmp/mysock", 0, $errno, $errstr);

   if (!$fp){
      echo  "Error: Could not open socket connection at " . $counter . "\n";
      exit;
   }else{    
      fputs ($fp, "hello", strlen("hello"));
      fclose($fp);
   }
}

For some reason, after a random number of connections (around 300-500) fsockopen will return with a warning Resource temporarily unavailable. In the beginning I was getting the warning at around 20-30 connections. But once I increased the backlog parameter in socket_listen it got a bit better to around 300-500. How do I overcome this?

出于某种原因,在随机数量的连接(大约300-500)之后,fsockopen将返回警告资源暂时不可用。一开始,我在大约20-30个连接处收到警告。但是一旦我增加了socket_listen中的backlog参数,它就会好一些,大约300-500。我该如何克服这个问题?

What is the way to build a php server socket to accept a lot of incoming connections per second (sustained).

构建一个php服务器套接字以便每秒接受大量传入连接(持续)的方法是什么。

Thanks!

The full error:

完整的错误:

PHP Warning: fsockopen(): unable to connect to unix:///tmp/mysock:0 (Resource temporarily unavailable) in test.php on line 22

PHP警告:fsockopen():无法连接到第22行的test.php中的unix:/// tmp / mysock:0(资源暂时不可用)

2 个解决方案

#1


Check your ulimit. Are you overflowing your file descriptor table?

EDIT: the backlog value you have in accept() is bogus. Most OS-es have the max incoming connection queue size on the scale of dozens, not thousands.

检查你的ulimit。你是否溢出了文件描述符表?编辑:你在accept()中的积压值是假的。大多数操作系统的最大传入连接队列大小为几十,而不是几千。

#2


I've just been looking at this issue (got here through Google) and I've found that a solution to get rid of the error:

我刚刚看到这个问题(通过谷歌来到这里),我发现了一个摆脱错误的解决方案:

PHP Warning: fsockopen(): unable to connect to unix:///tmp/mysock:0 (Resource temporarily unavailable) in test.php on line 22

..is to not use fsockopen() in the writer thread; try something like this instead:

..是不在编写器线程中使用fsockopen();尝试这样的事情:

if (! ($cSock = socket_create(AF_UNIX, SOCK_STREAM, 0))) {
    exit("Failed to create socket");
    continue;
} else if (! socket_connect($cSock, IPC_SOCK)) {
    exit("Failed to connect socket");
} else {
    $bw = socket_write($cSock, $msg);
    if ($bw === false) {
        exit("Socket write failed, %s", array(socket_strerror(socket_last_error())));
    } else {
        exit("Wrote $bw bytes to socket");
    }
}
socket_shutdown($cSock);
socket_close($cSock);

Better late than never ? ;-)

迟到总比不到好 ? ;-)

#1


Check your ulimit. Are you overflowing your file descriptor table?

EDIT: the backlog value you have in accept() is bogus. Most OS-es have the max incoming connection queue size on the scale of dozens, not thousands.

检查你的ulimit。你是否溢出了文件描述符表?编辑:你在accept()中的积压值是假的。大多数操作系统的最大传入连接队列大小为几十,而不是几千。

#2


I've just been looking at this issue (got here through Google) and I've found that a solution to get rid of the error:

我刚刚看到这个问题(通过谷歌来到这里),我发现了一个摆脱错误的解决方案:

PHP Warning: fsockopen(): unable to connect to unix:///tmp/mysock:0 (Resource temporarily unavailable) in test.php on line 22

..is to not use fsockopen() in the writer thread; try something like this instead:

..是不在编写器线程中使用fsockopen();尝试这样的事情:

if (! ($cSock = socket_create(AF_UNIX, SOCK_STREAM, 0))) {
    exit("Failed to create socket");
    continue;
} else if (! socket_connect($cSock, IPC_SOCK)) {
    exit("Failed to connect socket");
} else {
    $bw = socket_write($cSock, $msg);
    if ($bw === false) {
        exit("Socket write failed, %s", array(socket_strerror(socket_last_error())));
    } else {
        exit("Wrote $bw bytes to socket");
    }
}
socket_shutdown($cSock);
socket_close($cSock);

Better late than never ? ;-)

迟到总比不到好 ? ;-)