www.webdeveloper.com
Results 1 to 11 of 11

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

  1. #1
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304

    resolved [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 php.net with no luck, hope someone can help me here.

    The first try is as follows:
    PHP Code:
    $this->_sHost '127.0.0.1';
            
    $this->_iPort 11225;
            
            
    // open a client connection
            
    $this->_hSocket socket_create(AF_UNIXSOCK_STREAM0);        
            
    $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:
    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 "segmentation fault" error


    The second approach is using pfsockopen which seems to get me a little further:
    PHP Code:
    $fp pfsockopen ($this->_sHost$this->_iPort$errno$errstr);
            if (!
    $fp)
            {
                
    $result "Error: could not open socket connection";
            }
            else
            {
                
    // get the welcome message
                
    fgets ($fp1024);
                
    // write the user string to the socket
                
    fputs ($fp'Message ' __LINE__);
                
    // get the result
                
    $result .= fgets ($fp1024);
                
    // close the connection
                
    fputs ($fp"END");
                
    fclose ($fp);
                
    // trim the result and remove the starting ?
                
    $result trim($result);
                
    $result substr($result2);
                
    // now print it to the browser
            

    as it only returns connection refused:
    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?
    Please! use the PHP tags! or at least CODE tags
    (Code posted may work )
    --

  2. #2
    Join Date
    Sep 2009
    Posts
    93
    When specifying a numerical IPv6 address (e.g. fe80::1) you must enclose the IP in square brackets. For example, tcp://[fe80::1]:80.

  3. #3
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Pprakash View Post
    When specifying a numerical IPv6 address (e.g. fe80::1) you must enclose the IP in square brackets. For example, tcp://[fe80::1]:80.
    I didn't see any IPv6 in the code provided...it was all IPv4 with ports specified. I'm confused how this was relevant.

  4. #4
    Join Date
    Sep 2009
    Posts
    93
    if you can give full code of relayer.class.php, i can refer.

  5. #5
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304
    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

  6. #6
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304
    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?
    PHP Code:
    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 ($fp1024);
                
    // write the user string to the socket
                
    fputs ($fp'Message ' __LINE__);
                
    // get the result
                
    $result .= fgets ($fp1024);
                
    // close the connection
                
    fputs ($fp"END");
                
    fclose ($fp);
                
    // trim the result and remove the starting ?
                
    $result trim($result);
                
    $result substr($result2);
                
    // now print it to the browser
            
    }

        }
        
        public function 
    __destruct()
        {
        }
    }

    $oRelayer = new Relayer(); 
    Please! use the PHP tags! or at least CODE tags
    (Code posted may work )
    --

  7. #7
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    304

    resolved [Solved]

    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
    Please! use the PHP tags! or at least CODE tags
    (Code posted may work )
    --

  8. #8
    Join Date
    Sep 2009
    Posts
    93
    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

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Pprakash View Post
    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
    This is not an error in PHP. In other languages it might be an error. See the manual.

  10. #10
    Join Date
    Sep 2009
    Posts
    93
    you can also this manual for reference see this

  11. #11
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Pprakash View Post
    you can also this manual for reference see this
    That is for PHP4. From PHP5 the private|public|protected syntax is used in place of var.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles