SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post 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.

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by jemz View Post
    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?

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    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?
    PHP 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.

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    So you probably want something like this (completely untested):

    PHP Code:
    $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

  5. #5
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    So you probably want something like this (completely untested):

    PHP Code:
    $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.

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by jemz View Post
    Hi K.Wolfe ,

    Pointer means referencing to a variable?..


    Thank you in advance.
    Terminology mix up on my part 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.

  7. #7
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post

    Quote Originally Posted by K. Wolfe View Post
    Terminology mix up on my part 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.

  8. #8
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by jemz View Post
    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:

    PHP Code:
    while(;;) { // or while(true)
        //do stuff

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

    Code:
    #!/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)

  9. #9
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,436
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    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/feature...hp#example-389
    "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies."

  10. #10
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    Making your PHP script into a service:

    PHP Code:
    while(;;) { // or while(true)
        //do stuff

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

    Code:
    #!/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)

    Hi, I am using windows here...

    Thank you in advance.

  11. #11
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by jemz View Post
    Hi, I am using windows here...

    Thank you in advance.
    Oh, I'm sorry

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

  12. #12
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,145
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post

    Quote Originally Posted by K. Wolfe View Post
    Oh, I'm sorry

    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.

  13. #13
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by jemz View Post
    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.


Bookmarks

Posting Permissions

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