LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   PHP pfsockopen (http://www.linuxquestions.org/questions/programming-9/php-pfsockopen-552084/)

nathacof 05-07-2007 07:14 PM

PHP pfsockopen
 
Does anyone know of a good tutorial on using pfsockopen across multiple page loads?

http://dev.analext.com/socket_connect.phps

This seems to be the problem.
PHP Code:

if (!isset($_SESSION['socket'])){
  
$socket pfsockopen($host$port$errno$errstr10);
  
$_SESSION['socket'] = $socket;
} else {
$socket $_SESSION['socket'];} 

The variable $_SESSION['socket'] is getting set but it does not contain a valid file descriptor on subsequent page loads.

Could anyone see a reason why this shouldn't work?

nathacof 05-07-2007 10:03 PM

Basically I don't think this is possible. PHP is running as a module, however the persistent connection is associated with a specific process. Since Apache is multi-threaded in nature it's very likely that the connection is not available in the process I'm running my script in.

Without completely reconfiguring Apache on my dev server I don't know. Has any one had any experience with this in the past? I've updated my code and I believe everything should be working at this point.

:(

masterrahul 02-05-2009 04:22 AM

Hello nathacof

I am also facing the same problem; have you found any possible solution. I don't want to create socket again and again.
You can reply me at my mail
mastermailbox2001-manager@yahoo.com

nathacof 02-05-2009 07:58 AM

Masterrahul,

Unfortunately there is no way to maintain the same socket over multiple requests when running in a webserver context. If your application is running from the command line there would be no issue because the script would maintain the socket as long as it is executing, or you specifically kill it.

Sorry :\

masterrahul 02-06-2009 10:00 AM

Quote:

Originally Posted by nathacof (Post 3432998)
Masterrahul,

Unfortunately there is no way to maintain the same socket over multiple requests when running in a webserver context. If your application is running from the command line there would be no issue because the script would maintain the socket as long as it is executing, or you specifically kill it.

Sorry :\

Yes Now I understand, I changed my application to work in that way.

xhypno 02-06-2009 07:20 PM

Quote:

Originally Posted by nathacof (Post 3432998)
Masterrahul,

Unfortunately there is no way to maintain the same socket over multiple requests when running in a webserver context. If your application is running from the command line there would be no issue because the script would maintain the socket as long as it is executing, or you specifically kill it.

Sorry :\

This is wrong.

pfsock is a persistant socket the same way pconnect works for a mysql connection.

The dev resource is opened and when ever you call pfsock again it will call the same dev resource until the interpreter runs its gc which only takes things not used after 30 min. So yes it seems that you will be opening a new sock every time, but using pfsock it uses the smae resource when opened.

From PHP.net
"This function behaves exactly as fsockopen() with the difference that the connection is not closed after the script finishes. It is the persistent version of fsockopen()."


This would do the trick at the top of each script or the one opened over and over.

Code:

if(!socket){
    $socket = pfsockopen($host, $port, $errno, $errstr, 10);
}


nathacof 02-06-2009 07:26 PM

xhypno, have you read the comments on, http://php.net/pfsockopen/. I think you'll find it's a bit more complicated than you assume depending on how PHP is being executed. IE: ISAPI Module, PHP as a CGI, Apache Prefork or Worker MPM, all have different behavior in this regards.

xhypno 02-07-2009 11:00 AM

Quote:

Originally Posted by nathacof (Post 3434705)
xhypno, have you read the comments on, http://php.net/pfsockopen/. I think you'll find it's a bit more complicated than you assume depending on how PHP is being executed. IE: ISAPI Module, PHP as a CGI, Apache Prefork or Worker MPM, all have different behavior in this regards.

I have actually used it a lot. The only time it will be complicated is with Apache Prefork as each page call is in a new fork of apache and a new php interpreter. But the chances you are doing it in prefork is very slim because you only see that in large scale shared hosting environments that have been poorly designed where each user forks their own apache and php instance each time a page is accessed instead of chrooting them to one environment. You see this in a few of the Affinity/Hostway hosting services but that is about it because it is a very poor practice.

troykelly 09-16-2009 05:19 AM

It seems that when running php in almost any fashion on a web server - pfsockopen serves no purpose.

Each time the script is run, a new socket is instantiated.

It's fairly easy to test... run it - and see how many connections you have after a few refreshes.

xhypno - I would love to see an example of how you are running it so the sockets are re-used.

To be honest, I think it would be a bit of a security issue in a webserver environment anyway. How would you identify yourself as the rightful owner of the socket?

nathacof 09-16-2009 08:26 AM

Quote:

Originally Posted by troykelly (Post 3684831)
It seems that when running php in almost any fashion on a web server - pfsockopen serves no purpose.

It depends entirely on the SAPI in use by your version of PHP.
  • If you're using a thread safe version of PHP sockets should be available to re-use. (IIS, Apache Worker MPM)
  • With Apache's Pre-Fork MPM each httpd process only has access to it's own pool of resources, and each request may not be served by the same process.
  • With CGI you go through the whole PHP init, and shutdown routines every time you load a page, so there is no affect.
  • Using FastCGI however, should allow you to share persistent connections across all of your scripts.

Reference: http://devzone.zend.com/article/1021#Heading3

I'm not sure how this is a security concern. It's not like someone can take over your socket when it's in use. PHP just doesn't tear down the connection when you're done, so the person obtaining the persistent socket has no idea whether it was persistent or a brand new connection.

golden_boy615 02-29-2012 03:00 AM

thank you nathacof beracuse of your good illustration I have the same code like you with pfsockopen and no problem in apache I mean for each web page or session that opens on different client I have individual persistent socket and it does not close and everything goes fine and every session has its own socket but with on lighthttpd and fastcgi enable, my persistent socket will share for all web pages I mean all of different sessions have just one persistent socket .
as you said :
Quote:

Using FastCGI however, should allow you to share persistent connections across all of your scripts
I do not want fastcgi to share persistent connections across all of my sessions and I want each session has its own persistent socket.


All times are GMT -5. The time now is 06:06 PM.