Nev Posted June 19, 2013 Share Posted June 19, 2013 Hi all, I need a little help please. I've got some code that creates a socket and a echo server, but I'm not having any luck in getting it to run. I've got the socket created, and closes, but when I try to run the code inside the "Do" "While" section the web page just hangs. I can see the port created on my server. Can some please shed some light at to what I'm doing wrong or what settings I need to change. My server is Apache/2.0.64 (Win32) PHP/5.2.17 .code below. Thanks in anticipation. Nev <?php error_reporting(E_ALL); /* Allow the script to hang around waiting for connections. */ set_time_limit(0); /* Turn on implicit output flushing so we see what we're getting * as it comes in. */ ob_implicit_flush(); $address = '192.168.0.7'; $port = 10001; if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() failed: reason:1 " . socket_strerror(socket_last_error()) . "\n"; } if (socket_bind($sock, $address, $port) === false) { echo "socket_bind() failed: reason:2 $address" . socket_strerror(socket_last_error($sock)) . "\n"; } echo '<p>Hello World</p>'; if (socket_listen($sock, 5) === false) { echo "socket_listen() failed: reason:3 " . socket_strerror(socket_last_error($sock)) . "\n"; } do { if (($msgsock = socket_accept($sock)) === false) { echo "socket_accept() failed: reason:4 " . socket_strerror(socket_last_error($sock)) . "\n"; break; } /* Send instructions. */ $msg = "\nWelcome to the PHP Test Server. \n" . "To quit, type 'quit'. To shut down the server type 'shutdown'.\n"; socket_write($msgsock, $msg, strlen($msg)); do { if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) { echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n"; break 2; } if (!$buf = trim($buf)) { continue; } if ($buf == 'quit') { break; } if ($buf == 'shutdown') { socket_close($msgsock); break 2; } $talkback = "PHP: You said '$buf'.\n"; socket_write($msgsock, $talkback, strlen($talkback)); echo "$buf\n"; } while (true); socket_close($msgsock); } while (true); socket_close($sock); ?> Quote Link to comment https://forums.phpfreaks.com/topic/279343-do-while-true/ Share on other sites More sharing options...
trq Posted June 19, 2013 Share Posted June 19, 2013 This type of program needs to be run from the cli, not via apache. Essentially you are trying to run a server inside a server. Quote Link to comment https://forums.phpfreaks.com/topic/279343-do-while-true/#findComment-1436817 Share on other sites More sharing options...
Nev Posted June 20, 2013 Author Share Posted June 20, 2013 Thanks for your reply, I'm a bit new to this and I was wondering if you could explain "cli". Currently I'm trying to run from my laptop's web browser, through my local intranet. How should I run this program. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/279343-do-while-true/#findComment-1436989 Share on other sites More sharing options...
Irate Posted June 20, 2013 Share Posted June 20, 2013 Cli as in Client, I guess. And I wouldn't have a do {} while(true) running all the time, initialize a variable that holds the value true and change it during function execution so that you have a way to fall out of the loop. Quote Link to comment https://forums.phpfreaks.com/topic/279343-do-while-true/#findComment-1436990 Share on other sites More sharing options...
Nev Posted June 20, 2013 Author Share Posted June 20, 2013 Cool, thanks for the tip. I've managed to get this to work now, I had to open the port on my server pc and my laptop. Now I can telenet into the server. When I type "shutdown" it it stops the server, if I type quit it disconnects but does not shut down the server, so unable to start as already running. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/279343-do-while-true/#findComment-1436992 Share on other sites More sharing options...
trq Posted June 20, 2013 Share Posted June 20, 2013 Cli as in Client, I guess. Cli as in command line interface. See http://php.net/cli Quote Link to comment https://forums.phpfreaks.com/topic/279343-do-while-true/#findComment-1437004 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.