Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-19-2013, 10:06 PM   PM User | #1
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
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>
sorlaker is offline   Reply With Quote
Old 01-19-2013, 11:59 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,496
Thanks: 44
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 01-20-2013, 12:37 AM   PM User | #3
sorlaker
Regular Coder

 
Join Date: Dec 2009
Posts: 166
Thanks: 23
Thanked 1 Time in 1 Post
sorlaker has a little shameless behaviour in the past
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 01:03 AM..
sorlaker is offline   Reply With Quote
Old 01-20-2013, 03:07 AM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,496
Thanks: 44
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 01-20-2013, 04:45 AM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:41 AM.


Advertisement
Log in to turn off these ads.