>I'm trying to setup a basic TCP server which I will later expand. Right now >I get this error on the loop after it receives data the first time: > >Warning: socket_read() unable to read from socket [317]: in >f:\public\www\josiah\josiah2\consultation\chat\test_server2.php on line 43 >socket_read() failed: reason: Incorrect function.
Your code works perfectly on my Linux box... The first time I ran it. After a 'shutdown' (of your server, not the whole box) though, it bitched about socket_bind and an already used address... Sometimes. It seems to be depending on whether I started it with or without & and then ^C or not, and 'shutdown' seems to only work sometimes... That doesn't seem right to me... :-) So maybe you have old processes hanging around, spewing out error messages to MS-DOS prompts that are long-gone? Maybe just try a re-boot of the whole box... It is Windows, after all. Does socket_close() have a return value to indicate success/failure? Don't end the loop if it didn't really close, I guess. > >Any ideas? See the server code below. > ><?php > /* > * We don't want any time-limit for how the long can hang > * around, waiting for connections: > */ > set_time_limit(0); > >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 (); Like, I think ob_XXXX only affects stuff you echo out to the BROWSER (aka stdout). I don't think it's gonna do squat for your TCP server... Won't hurt, though... Or is this just for that echo line way down below? I would do: echo $tcp; flush(); personally... But I'm old and don't trust the new functions as much :-) > >$address = '172.25.190.4'; >$port = 10003; > >if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) { > echo "socket_create() failed: reason: " . socket_strerror ($sock) . >"\n"; >} > >if (($ret = socket_bind ($sock, $address, $port)) < 0) { > echo "socket_bind() failed: reason: " . socket_strerror ($ret) . "\n"; >} > >if (($ret = socket_listen ($sock, 5)) < 0) { > echo "socket_listen() failed: reason: " . socket_strerror ($ret) . "\n"; >} > >do { > if (($msgsock = socket_accept($sock)) < 0) { > echo "socket_accept() failed: reason: " . socket_strerror ($msgsock) >. "\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))) { > echo "socket_read() failed: reason: " . socket_strerror ($ret) . >"\n"; > break 2; > } > if (!$buf = trim ($buf)) { This ain't right... No, maybe it is... You want an assignment there, but you really ought to be doing the trim() on 'quit ' as well, to be nice to bad typists like me :-) And some parens around the assignment would be nifty. > 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); >?> -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php