Im trying to read input from an open port using socket_read(). Its actually the exact demo script in the manual (changed to my IP address). Um, The problem im having is that its only reading one letter at a time and not reading returns or spaces. Im thinking it may have something to do with IIS or just the fact that im on a windows machine. Can someone give me a pointer or two?
-Scott
This is the code:
<?php
error_reporting (E_ALL);
set_time_limit (0);
ob_implicit_flush ();
$address = '10.0.0.198';
$port = 23;
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 = "\r\nWelcome to PHP telnet server. \r\n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\r\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (FALSE === ($buf = socket_read ($msgsock, 2048, PHP_NORMAL_READ))) { // added PHP_NORMAL_READ
echo "socket_read() failed: reason: " . socket_strerror ($ret) . "\r\n";
break 2;
}
if (!$buf = trim ($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close ($msgsock);
break 2;
}
$talkback = "$buf";
socket_write ($msgsock, $talkback, strlen ($talkback));
echo "$buf\r\n";
} while (true);
socket_close ($msgsock);
} while (true);
socket_close ($sock);
?>