Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post

    Use same socket to communicate

    Hello guys. I made this code to send commands to another machine. But every time i wanna send a command it creates a new socket to do that. Whats the best way to use the same socket to communicate?

    PHP Code:
    <?php
        
    if ($_POST){
            
    $sock socket_create(AF_INETSOCK_STREAM0);
            
    socket_connect($sock"127.0.0.1"6000);
            
    socket_send($sock$_POST["command"], strlen($_POST["command"]), 0);
        }
    ?>
    <form action="message.php" method="post">
        <input type="text" name="command"/><input type="submit"/>
    </form>

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,441
    Thanks
    62
    Thanked 537 Times in 524 Posts
    In short, given the code you've shown, you can't. Every time the php script terminates it will close the socket.

    The only way you can do it is to keep the script cycling in a loop. Open the socket before the loop and then inside the loop, keep checking a database table for new commands etc that you want to send via the connection. When the script is ready to close the connection you can break out of the loop and then close it.

    As for the GUI, you'd need another page which you can input your commands on, submit and they are put into the database for the socket script to pick up. If you wanted to really pimp it up you could use ajax so that you don't have to keep refreshing the page.

    Note that you could actually just use the session instead of the DB but with the DB at least you'd have a log of everything.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post
    I've already thought about that. Is there a better way?

    edit 1: Otherwise it'd be better to create a new socket every time i send a command.
    Last edited by sorlaker; 01-20-2013 at 02:03 AM.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,441
    Thanks
    62
    Thanked 537 Times in 524 Posts
    No there isn't a better way.

    It's the very nature of how PHP works. It's a reactive language. When you make a request to a php script, it runs and then stops. Thats it because thats the way http works. Http doesn't keep a connection open and continue swapping request data and html. It's a request and reply service.

    I'm afraid the only thing you can do is to create a cycling script which keeps the socket open in a loop and communicate with it via another script or use multiple page requests.

    There really isn't a miracle method here to doing it. It's just not designed that way.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP itself is capable of running interactive; its the http protocol that is stateless.
    To do what you want to do, and not clog up the httpd, than you would want to write a socket handler and execute it on an infinite loop in the background. So launch that on the command line. It then needs to store any information and pick any information up through a file or db. You can pipe directly to the running process, but frankly simply writing a queue of commands into a filesystem file is a lot easier to do in PHP than piping into a running process.
    This is why I have suggested the use of Java RMI since they establish persistent connections.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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