stuffradio Posted July 12, 2009 Share Posted July 12, 2009 Is there any way to keep a socket server alive? I have a socket server that runs fine, and does what I need it to... but when I close the window that has the socket server in it, it stops the server. How can I prevent this from happening? Quote Link to comment https://forums.phpfreaks.com/topic/165666-keep-socket-server-alive/ Share on other sites More sharing options...
trq Posted July 12, 2009 Share Posted July 12, 2009 Put the server into a background process. eg; socket.php & Quote Link to comment https://forums.phpfreaks.com/topic/165666-keep-socket-server-alive/#findComment-873887 Share on other sites More sharing options...
stuffradio Posted July 12, 2009 Author Share Posted July 12, 2009 Running sudo ./socket.php & does not keep it in the process list. Any idea why that would be? Quote Link to comment https://forums.phpfreaks.com/topic/165666-keep-socket-server-alive/#findComment-873908 Share on other sites More sharing options...
trq Posted July 12, 2009 Share Posted July 12, 2009 It could be the way your server is built. Can we see some code? Quote Link to comment https://forums.phpfreaks.com/topic/165666-keep-socket-server-alive/#findComment-873909 Share on other sites More sharing options...
stuffradio Posted July 12, 2009 Author Share Posted July 12, 2009 <?php mysql_connect("localhost", "root", "admin"); mysql_select_db("awc"); // set some variables $host = "192.168.0.197"; $port = 2100; // don't timeout! set_time_limit(0); // 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"); // accept incoming connections // spawn another socket to handle communication $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); // read client input $input = socket_read($spawn, 1024) or die("Could not read input\n"); // clean up input string $input = trim($input); // reverse client input and send back $output = strrev($input) . "\n"; socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); mysql_query("INSERT INTO `socket` (socketdata) VALUES ('$input')"); // close sockets socket_close($spawn); socket_close($socket); ?> Quote Link to comment https://forums.phpfreaks.com/topic/165666-keep-socket-server-alive/#findComment-873921 Share on other sites More sharing options...
trq Posted July 12, 2009 Share Posted July 12, 2009 Yeah, that ain't going to work. You need to implement socket servers within a loop so that they continue listening while they loop. Quote Link to comment https://forums.phpfreaks.com/topic/165666-keep-socket-server-alive/#findComment-873926 Share on other sites More sharing options...
stuffradio Posted July 13, 2009 Author Share Posted July 13, 2009 I found this socket server now that does have a loop, but whenever it receives a client message then it dies. Where in here can I keep it alive? <?php error_reporting(E_ALL); mysql_connect("localhost", "root", "admin"); mysql_select_db("awc"); // don't timeout set_time_limit (10000); // set some variables $host = "192.168.0.197"; $port = 2100; // 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"); // keep looping and looking for client input do { // read client input $input = socket_read($spawn, 1024, 1) or die("Could not read input\n"); if (trim($input) != "") { echo "Received input: $input\n"; mysql_query("INSERT INTO `socket` (socketdata) VALUES ('$input')"); // if client requests session end if (trim($input) == "END") { // close the child socket // break out of loop socket_close($spawn); break; } // otherwise... else { // reverse client input and send back $output = strrev($input) . "\n"; socket_write($spawn, $output . "? ", strlen (($output)+2)) or die("Could not write output\n"); echo "Sent output: " . trim($output) . "\n"; } } } while (true); // close primary socket socket_close($socket); echo "Socket terminated\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/165666-keep-socket-server-alive/#findComment-874323 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.