CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Issue connecting PHP server class to local socket

  1. #1
    Join Date
    Oct 2014
    Posts
    5

    Issue connecting PHP server class to local socket

    Hi All,

    I have a Ubuntu 14 with apache2 web server and are trying to write a web service that connects to a local TCP socket (this is the only method to interface to this particular daemon running).

    I have checked and selinux is disabled by default in ubuntu and have verified the port is available on 127.0.0.1 and have also done a simple telnet check that it accepts connections within the machine. However I can't get my PHP code as the TCP client to connect to the TCP Server, it keeps saying connection refused. The class with the connection method is as follows:

    PHP Code:
    <?php
        
    class CABHCmdInt
        
    {
            
    // Socket Variables
            
    private $m_Port;
            private 
    $m_Socket;
            private 
    $m_Created;
            private 
    $m_Connected;
            
            
    // State Variables
            
    private $m_SessionKey;
            
            function 
    _construct($pPort)
            {
                
    $this->m_Port $pPort;  // Needs to be a string
                
    $this->m_Created false;
                
    $this->m_Connected false;
                
                
    $this->m_SessionKey ""// Empty string indicates no session established
            
    }
            
            function 
    _destruct()
            {
                if (
    $this->m_Connected == true)
                    
    socket_close($this->m_Socket);
            }
            
            public function 
    connectCommandInterface ()
            {
                
    // This will start the command interface to the local port
                // NOTE: It does not set-up a session (key exchange) only does the 
                // TCP Connect part.
                
    if ($this->m_Created == false)
                {
                    
    $this->m_Socket  socket_create(AF_INETSOCK_STREAMSOL_TCP)
                    if (
    $this->m_Socket === false)
                        return 
    false;
                    
    $this->m_Created true;
                }
                if (
    $this->m_Connected == false)
                {
                    
    // We only ever to a local server for this project
                    
    $time time();
                    while (!@
    socket_connect($this->m_Socket"127.0.0.1"$this->m_Port))
                    {
                        
    $err socket_last_error($this->m_Socket);
                        if (
    $err == 115 || $err == 114)
                        {
                            if ((
    time() - $time) >= $timeout)
                            {
                              
    socket_close($this->m_Socket);
                              
    // Connection Timeout
                              
    error_log("ABH Command Interface connection timeout"0);
                              return 
    false;
                            }
                            
    sleep(1);
                            continue;
                        }
                        
    $reason socket_strerror($err);
                        
    error_log("ABH Connect Command Interface error on connect, reason " $reason0);
                        return 
    false;
                    }
                    
    $m_Connected true;
                }
                if (
    $this->m_Connected && $this->m_Created)
                    return 
    true;
                else
                    return 
    false;
            } 
    // End of connect function
        
    }
    ?>
    I have searched the internet high and low but all I can find is talk about selinux which should not be a problem if it is disabled.

    The apache error log shows the message from the error log line as :
    [Wed Oct 29 21:51:26.644788 2014] [:error] [pid 1630] [client 127.0.0.1:36954] ABH Connect Command Interface error on connect, reason Connection refused, referer: http://127.0.0.1/api/CITesting.php

    Any ideas on how I can get this to work or why a PHP class in apache is refused connection would be great.

    Thanks in advance.

    codeAutomationGuy

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Issue connecting PHP server class to local socket

    Nothing wrong with the code. Did you modify ufw to allow connections via port 36954?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Oct 2014
    Posts
    5

    Re: Issue connecting PHP server class to local socket

    Thanks PeejAvery for the suggestion, running the command "sudo ufw status verbose" shows it as inactive and since a local telnet client works but what I realized is the calling function that constructed the class did not do it correctly and this was not the target port number. Solved now thanks for the suggestions, made me realise the error.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)