Hello,
I am writing a service which requires a server and a client, both running on the same machine. The server will run “forever” while the client is a middle-man for an ajax service.
Typically, the middle-man will connect to the “forever” and send or wait for data, the “forever” will process that data and return something back to the middle-man but it will also send something to another middle-man.
The “forever” is very basic at this point but it works just fine, tested it with a telnet client.
the middle-man I cant get it to connect, I am trying two approaches and maybe the error comes from my lack of experience/knowledge. I have searched this forum, google and [URL=”http://www.php.net/manual/en/book.sockets.php”]php.net
The first try is as follows:
[code=php]$this->_sHost = ‘127.0.0.1’;
$this->_iPort = 11225;
// open a client connection
$this->_hSocket = socket_create(AF_UNIX, SOCK_STREAM, 0);
$this->_iPort = 11225;
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”;
}
I have tried changing _sHost to tcp://127.0.0.1, tcp://192.168.0.199, tcp://localhost, localhost, 127.0.0.1, 192.168.0.199… it always returns:
[QUOTE]
Warning: socket_connect(): unable to connect [2]: No such file or directory in relayer.class.php on line 27
socket_connect() failed.
Reason: () No such file or directory
This one occasionally gives me a “[U]segmentation fault
The second approach is using pfsockopen which seems to get me a little further:
[code=php]
$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
}
as it only returns connection refused:
[quote]
Warning: pfsockopen(): unable to connect to tcp://127.0.0.1:11225 (Connection refused) in relayer.class.php on line 33
.
At all times the “forever” is running and I am able to reconnect at any time with the telnet client, the folder where both files (forever and middleman) are has full write permissions as well.
(Im running Ubuntu 9.10)
Any clue here?