Connection need help please

Hi, can i ask how can i accept another connection if the “stream_socket_accept” don’t have connection on a socket anymore ?..

Thank you in advance.

In a loop, do a check to see if you’ve lost your connection, if so, just fire off a function that returns the resource back to you again. You can use a pointer and a wrapper to make sure the resource is tied back to the same variable. Do you have some sample code?

Hi K.Wolfe,

what do you mean by this

You can use a pointer and a wrapper to make sure the resource is tied back to the same variable

Do you have some sample code?


$file = fopen('textfile.log','a+');
$server = stream_socket_server("tcp://serverIP:Port", $errno, $errorMessage);

if (!$server) {
 echo "Cannot bind server";
}
else{
    while($client = @stream_socket_accept($server)){
        stream_copy_to_stream($client, $file);
        fclose($file);
        fclose($server);

    }

}


Thank you in advance.

So you probably want something like this (completely untested):


$file = fopen('textfile.log','a+');
$server = stream_socket_server("tcp://serverIP:Port", $errno, $errorMessage);

if (!$server) {
    throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
} else {
    $client = stream_socket_accept($server);
    while(;;) {
        if(!$client || !stream_copy_to_stream($client, $file)) {
            stream_socket_accept($server);
        }
    }
}

Just find a way to reestablish your connection to the same variable and try again. I didn’t put any pointer logic in, but here’s some reading on it: http://www.php.net/manual/en/language.references.php

Hi K.Wolfe ,

Pointer means referencing to a variable?..

Thank you in advance.

Terminology mix up on my part :slight_smile: I meant to say “pass a referenced variable to the function” (&$foo) which would allow you to directly affect the variable without having to return it.

Hi K.Wolfe,

Can i ask how can you run this in background ?..I want to run this in background just acting like a services.Is this possible?

Thank you in advance.

Making your PHP script into a service:


while(;;) { // or while(true)
    //do stuff
}

Make a bash script to launch your php script, and relaunch it if it crashes:


#!/bin/bash
if [[ -z "$1" ]];then
    echo "must supply app name"
    exit 1
fi

d="/var/lock/process_monitor_$1"
d+=".lock"
exec 9>$d
    if ! flock -n 9  ; then
    exit 1
fi

while (( 0 == 0 ));do
    $@ &
        pid=`jobs -l | awk '{print $2}'`
        wait $pid
done  

You’ll want to launch this upon startup of your server. Theres a portion that prevents your app from being launched more than once. Normally this is run like so ‘sh process_monitor.sh myapp’, but I’m not entirely sure how to get this to work this php, I usually use it for binaries (they dont require php infront of the file name to run it)

You can set PHP scripts to be executable and add the path to the PHP binary at the top of the script, then you can call the script without having to prefix it with 'php ':
http://www.php.net/manual/en/features.commandline.usage.php#example-389

Hi, I am using windows here…

Thank you in advance.

Oh, I’m sorry :slight_smile:

Someone else will have to step in to help, but I’d recommend moving to linux to run a production environment w/ PHP

Hi,K.Wolfe,

Thank you for the reply,I just want to ask is there a down side using windows in production ?..mostly chooses Linux…
I will try to change our production to Linux.

Thank you in advance.

Well… to kind of back step here… I can’t think of any technical limitations of running on Windows other than Linux is more commonly used in a server environment. You’ll find more documentation around issues you come across (case n point would be this thread). Linux itself is sort of an IDE. Everything at its core enables you to efficiently run services. In Windows, those features usually come from .NET solutions.

So. Don’t switch over to Linux unless you feel you need to here. I don’t want to suggest something you don’t need. I suggest that you look in to learning a bit more about linux, as it is more the standard for deploying services / servers. Once you’ve become comfortable with it, you’ll find that you have more tools at your disposal.