gaza165 Posted December 7, 2009 Share Posted December 7, 2009 I am trying to formulate a script that can output strings using sockets. so far i have this code that creates and provides the user with a welcome message. <?php // don't timeout set_time_limit (0); // set some variables $host = "192.168.81.130"; $port = 3333; // create socket $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); // bind socket to port $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); // start listening for connections $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); echo "Waiting for connections...\n"; // accept incoming connections // spawn another socket to handle communication $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); echo "Received connection request\n"; // write a welcome message to the client $welcome = "Roll up, roll up, to the greatest show on earth!\n? "; socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send connect string\n"); ?> i then open another file that gets the welcome message and displays it to the user. <?php $host="192.168.81.130"; $port = 3333; // open a client connection $fp = pfsockopen ($host, $port, $errno, $errstr); if (!$fp) { $result = "Error: could not open socket connection"; } else { // get the welcome message $result .= fgets ($fp, 1024); } ?> Server said: <b><? echo $result; ?></b> This works fine, however, once the script has finished the connection the to the socket server is close, how do i keep the session persistently open to keep feeding and outputting new strings?? Thanks Garry Quote Link to comment https://forums.phpfreaks.com/topic/184214-need-help-with-sockets-in-php/ Share on other sites More sharing options...
JAY6390 Posted December 7, 2009 Share Posted December 7, 2009 You'd need to have a loop of some sort (for/while/do while) and just keep looping until the connection is broken then break out of the loop and clean up Quote Link to comment https://forums.phpfreaks.com/topic/184214-need-help-with-sockets-in-php/#findComment-972582 Share on other sites More sharing options...
gaza165 Posted December 7, 2009 Author Share Posted December 7, 2009 Can anyone point me in the right direction on how to do that?? I need a loop to keep the connection open to look for new messages??? Quote Link to comment https://forums.phpfreaks.com/topic/184214-need-help-with-sockets-in-php/#findComment-972729 Share on other sites More sharing options...
premiso Posted December 7, 2009 Share Posted December 7, 2009 You want to look into AJAX, using AJAX you can make server calls and it can re-run the connection and check for new messages. Other than that you have to do a forced reload, as even just sleep the script while setting the set_time_limit to infinite will slow down your server a ton by eating up the memory. Better to do the AJAX calls and get the data that way. Quote Link to comment https://forums.phpfreaks.com/topic/184214-need-help-with-sockets-in-php/#findComment-973004 Share on other sites More sharing options...
MadTechie Posted December 7, 2009 Share Posted December 7, 2009 If you have threaded apache then your connection will close when the script terminates, try using non-threaded, however premiso suggestion would be better! Quote Link to comment https://forums.phpfreaks.com/topic/184214-need-help-with-sockets-in-php/#findComment-973016 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.