How to prevent lighttpd and fastCGI to share persistent sockets for different session

Discussion in 'PHP' started by golden_boy615, Feb 29, 2012.

  1. #1
    How to prevent lighttpd and fastCGI to share persistent sockets for different sessions
    I want to have persistent socket to send and receive data to a program written in C language on embedded linux on arm9 processor (this program has no problem in making and serving socket I tested it with other applications)
    this is my code:
     <?php
    SESSION_START(); 
    error_reporting(0);
    @ini_set(‘display_errors’, 0);
    
    if(!$_SESSION['randa']){
    $_SESSION['randa'] = rand(1,99999);
    }
    echo $_SESSION['randa'] .'<br>';
    
    $fp_a = pfsockopen("192.168.1.32",7777, $errno, $errstr, 30);
    
    $outa = "D-1;".$_SESSION['randa'].';';
    fwrite($fp_a, $outa);
    $out_putt = fread($fp_a, 2048);
    echo $out_putt ; 
    
    ?>
    <meta http-equiv="refresh" content="2;url=?">
    
    
    PHP:
    as you see it reloads every 2 seconds. My goal is to prevent closing socket due to the PHP script exit and each session has its own persistent opened socket because of some reasons in my project,The above code works fine with apache and every session have their own persistent socket with no problem, the problem rises when I put this script in lighttpd with fastCGI enabled.
    lighttpd opens persistent socket and everything goes fine for one session but if more that one session (web pages on different PCs) open, lighttpd share this persistent socket for all of them and does not open new socket for different sessions and all of them read each others data and sends their own data to one opened socket .
    I used even this script too:
     <?php
    session_start(); 
    if(!$_SESSION['views']){
    $_SESSION['views'] = pfsockopen("192.168.1.32",7777, $errno, $errstr, 30);
    $fp = $_SESSION['views'] ;
    stream_set_blocking ( $fp , 0 ) ;
    }else{
    $fp = $_SESSION['views'] ;
    }
    $sread=fread( $_SESSION['views'], 1024);
    if($sread==false and $sread!=''){
    $_SESSION['views'] = pfsockopen("192.168.1.32",7777, $errno, $errstr, 30);
    $fp = $_SESSION['views'] ;
    stream_set_blocking ( $fp , 0 ) ;
    }
    if ($fp) {
    $out = "D-1;";
    fwrite($fp, $out);
    $out_put = $sread.fread($fp, 2048);
    echo $out_put ; 
    }
    ?>
    
      <meta http-equiv="refresh" content="2;url=?">
    
    PHP:
    to put persistent socket in session variable to prevent using same socket for different sessions but nothing changed and everything goes the same as first script did (one socket for all sessions ).

    How can I prevent this problem? which configuration should I change for what to enable one socket for one session ??
     
    golden_boy615, Feb 29, 2012 IP
  2. trendint

    trendint Peon

    Messages:
    52
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well there are two problems here;

    1) pfsockopen will open a single connection per process - this means that the persistence is only handled for one thread - and in the case of FastCGI, PHP is pre-forked to multiple threads. So if your site has let's say 5 threads running pre-forked, then a request to the site will be answered by one thread, the next request will be handled by a second thread - at this point two separate pfsockopen connections have been made. If your C program on the other side is incapable of handling this scenario, there can be overhead issues with this.

    2) pfsockopen can't be session based; it's either you are connected or you are not. Maintaining a session can be useful if you are,for example, trying to ensure that a person is authenticated to use the connection, but it won't really do much in maintaining the open connection to the service.

    Are you trying to keep the connection persistent for the sake of reducing the handshake time? Or is it more that you want to complete a multi-step process that the C program requires to happen in one step?

    Without knowing your actual needs; a possible solution would be to create a middle man abstraction layer - a PHP script that runs standalone as a daemon (more info here) that does two things; first it maintains a global persistent connection to your service, and secondly can accept simple POST's from your main website to interact with the always connected C program. It would in essence become an API that you can interact with easily from the web side.
     
    trendint, Feb 29, 2012 IP
  3. golden_boy615

    golden_boy615 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have to say thank you for your answer but:
    1) I did not get what you want to say, FastCGI does support what I want or not (individual persistent socket connection for each opened web page) but if I got you scenario right my C program can handle this scenario that you said I mean can handle multiple threads or connections for requests I tested it with other C programs (I mean I sent requests with other C programs for test to it) and if pay attention to what I said I tested exactly with that script that I posted here on apache web server and it was ok with no problem.
    2) Thank you for your second information I did not know your second note so I will be informed that pfsockopen can't be session based
    3) I need persistent connection because of my C program has to be informed that each received request from web is from web page opened before OR from a new web page opened just now to prevent sending repetitive data to web pages that opened before (some data can be repetitive but reducing them can reduce overhead but some of them must not sent twice to a single web and it could cause huge problems) and to reduce a huge interaction and data traffic between C program and web which is really necessary for me because I am working on an embedded arm9 project not on a huge web server and reducing any single overhead even a byte can be a grate bless
     
    golden_boy615, Mar 1, 2012 IP