PHP Sockets Errors (connection refused and No such file or directory)
Posted
by Purefan
on Stack Overflow
See other posts from Stack Overflow
or by Purefan
Published on 2010-05-06T08:08:15Z
Indexed on
2010/05/06
11:38 UTC
Read the original article
Hit count: 202
Hello all,
I am writing a server app (broadcaster) and a client (relayer). Several relayers can connect to the broadcaster at the same time, send information and the broadcaster will redirect the message to a matching relayer (for example relayer1 sends to broadcaster who sends to relayer43, relayer2 -> broadcaster -> relayer73...)
The server part is working as I have tested it with a telnet client and although its at this point only an echo server it works.
Both relayer and broadcaster sit on the same server so I am using AF_UNIX sockets, both files are in different folders though.
I have tried two approaches for the relayer and both have failed, the first one 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 (its running from command line) it often also returns a segmentation fault.
The second approach is using 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 only 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 have tried with different host names, 127.0.0.1, localhost, tcp://127.0.0.1, 192.168.0.199, tcp://192.168.0.199, none of it has worked.
Any ideas on this?
© Stack Overflow or respective owner