/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Socket errors (connection refused and No such file or directory)

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[/URL] with no luck, hope someone can help me here.

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”;
}[/code]

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

[/QUOTE]

This one occasionally gives me a “[U]segmentation fault[/U]” error

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
}
[/code]

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

[/quote]

.
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?

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@PprakashMay 05.2010 — When specifying a numerical IPv6 address (e.g. fe80::1) you must enclose the IP in square brackets. For example, tcp://[fe80::1]:80.
Copy linkTweet thisAlerts:
@criterion9May 05.2010 — When specifying a numerical IPv6 address (e.g. fe80::1) you must enclose the IP in square brackets. For example, tcp://[fe80::1]:80.[/QUOTE]

I didn't see any IPv6 in the code provided...it was all IPv4 with ports specified. I'm confused how this was relevant.?
Copy linkTweet thisAlerts:
@PprakashMay 05.2010 — if you can give full code of relayer.class.php, i can refer.
Copy linkTweet thisAlerts:
@purefanauthorMay 05.2010 — Hey all, thank you for your comments.

Theres not much else to relayer.class.php that code is in the constructor and no other functions yet (project is starting), I will however post it tomorrow as soon as I get to the office.

If it helps, Im calling relayer from the command line.

Again, thank you
Copy linkTweet thisAlerts:
@purefanauthorMay 06.2010 — As requested here is the entire Relayer class, really hope someone can help here, would you say its easier if it werent a AF_UNIX socket?
[code=php]
class Relayer
{
private $_hSocket;
private $_iPort;
private $_sHost;
private $_sFile;
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";
}*/
$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
}

}

public function __destruct()
{
}
}

$oRelayer = new Relayer();
[/code]
Copy linkTweet thisAlerts:
@purefanauthorMay 06.2010 — After more through check I found out one of the firewalls was not letting my connections go, that was it...

Sorry to have wasted your time
Copy linkTweet thisAlerts:
@PprakashMay 07.2010 — after refering your given code, i observe the one error,


that is to declare variables in constructin a class in php, we use key word "var"

so change the below

private $_hSocket;

private $_
iPort;

private $_sHost;

private $_
sFile;

as

private var $_hSocket;

private var $_
iPort;

private var $_sHost;

private var $_
sFile;

and then try agian
Copy linkTweet thisAlerts:
@criterion9May 07.2010 — after refering your given code, i observe the one error,


that is to declare variables in constructin a class in php, we use key word "var"

so change the below

private $_hSocket;

private $_
iPort;

private $_sHost;

private $_
sFile;

as

private var $_hSocket;

private var $_
iPort;

private var $_sHost;

private var $_
sFile;

and then try agian[/QUOTE]

This is [B][U]not[/U][/B] an error in PHP. In other languages it might be an error. See the manual.
Copy linkTweet thisAlerts:
@PprakashMay 07.2010 — you can also this manual for reference see this
Copy linkTweet thisAlerts:
@criterion9May 07.2010 — you can also this manual for reference see this[/QUOTE]

That is for PHP4. From PHP5 the private|public|protected syntax is used in place of var.
×

Success!

Help @purefan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...