简体   繁体   中英

PHP persistent socket connection

I have the following architecture.

  • A Windows application which can talk with external software through a defined port.
  • A webapplication running on the same localhost.
  • PHP-script serving as connector between these two sides.

I make from the webapplication AJAX-request to php-script which should make connection with the windows application and sent to the webapplication its response.

It works nice. But only one time. The second call to php-script already doesn't work because the window application deny access to php-script. I tryed to use pfsockopen so:

if(!isset($fp)) {
    $fp = pfsockopen($address, $service_port, $errno, $errstr, 30);    
}
if(!$fp) die($errstr);

fwrite($fp, $out);

But window application deny it when I call it second time. What should be the right approach/solution?

Something like let php script runs as a server keeping connection? Do you have an idea or example? Thank you.

To me it seems like after first time of successful connection you are trying to connect your windows application on same port. Are you changing port number? If you create a persistent connection its likely that windows will keep that port assigned to your first connection. Then it can't use same port for a while. And this explains why it works first time try but not after. Try to use fsockopen instead of persistent one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM