Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

confused about fsockopen

not sure I understand the syntax

9:51 pm on Sep 4, 2010 (gmt 0)

New User

5+ Year Member

joined:May 3, 2007
posts: 27
votes: 0

Hi everyone. I'm tasked with exchanging data with another machine via TCP. I have a pretty good understanding of web architecture overall, but working with sockets is new to me and I seem to be missing something. When I use this, it fails:

$remote_machine = fsockopen($destination,$port,$error_number,$error_message,5) or die($error_message . " " . $error_number);

is an IP address, and
in this case is 63333.)

When executed, this fails with 'Connection refused (111)'. Which makes sense, since this system requires username / password authentication.

What I'm failing to understand is how I get the username and password to the system. I've read several 'tutorials' online and they all show this information being passed after the connection is established.

If I can get authenticated, I must then receive and respond to some JSON formatted data, if that's noteworthy.

Can someone help me understand the process here?
10:56 pm on Sept 4, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Feb 11, 2006
posts: 481
votes: 0


TCP is operating at a lower level than whatever type of authentication you are using so the tutorials you've looked at are right the connection has to be established first.
Connection refused means the socket couldn't be established, this could be for a number of reasons:
  • There's no machine with the IP address specified.
  • There's no program listening on the port specified.
  • The program listening only accepts connections from the localhost/specific ip addresses
  • A firewall somewhere in between the two machines is blocking the connection.

And probably others I can't think of at the moment.
If you have access try using a program like
to make the same connection, see if it gives you the same error.

3:52 pm on Sept 5, 2010 (gmt 0)

New User

5+ Year Member

joined:May 3, 2007
posts: 27
votes: 0

Thanks, Little-G. It turned out that I had been given the wrong port number, which explained the connection refusal. Everything is working now.

The syntax of the commands had me confused. It seemed to me that opening a connection inherently involved authentication, so I could not see how I could open a connection and then authenticate myself.

Kind of like a speakeasy operator letting you into the bar and then asking you for the secret password. Didn't make sense.

Apparently, in this case, "opening" the connection (fsockopen) is the equivalent of simply knocking on the door. So now it makes sense. :)