PHP socket errors (connection refused and no such file or directory)
I am writing a server application (broadcaster) and client (relayer). Several relays can simultaneously connect to the broadcaster, send information, and the broadcaster forwards the message to the corresponding relay (for example, relayer1 sends to the broadcaster, which sends to relayer43, relayer2 β broadcaster β relayer73 ...)
The server side works as I tested it with a telnet client and although it only works with an echo server at the moment.
Both repeater and broadcaster sit on the same server, so I am using AF_UNIX sockets, however both files are in different folders.
I have tried two approaches for a relier and they both fail, the first is using 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";
}
This returns "Warning: socket_connect (): unable to connect [2]: no such file or directory in relayer.class.php on line 27" and (running it from the command line) it often returns a segmentation fault as well.
The second approach uses 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
}
which returns the error "Warning: pfsockopen (): unable to connect to tcp: //127.0.0.1: 11225 (connection refused) in relayer.class.php on line 33"
In all tests I tried with different hostnames, 127.0.0.1, localhost, tcp: //127.0.0.1, 192.168.0.199, tcp: //192.168.0.199, none of them worked.
Any ideas on this?
a source to share