I had this problem just a few weeks ago and my 'rhoids are still hurting. The specific problem I had was that there was no hard eof() to work with. A wonderful person made a suggestion that turned out to actually work.
Consider this code: (note that I'm changing the IP to protect the innocent) and adding comments.
<?php
$fp = fsockopen("xxx.xx.xxx.xx", 8111, $errno, $errstr);
if (!$fp) {
echo "ERROR: $errno - $errstr<br>\n";
} else {
****Note: this is the string that I'm building to send:
$s_tni = "323555xxxx";
$s_site = "BSA";
$s_junk = "Retrieve#";
$s_junk .= $s_tni;
$s_junk .= $s_site;
$s_junk .= "/n";
sleep(1);
****Note: this is the login I'm sending. The sleep() is to give him time to respond.
fwrite($fp,"TELExxxxxxx\n");
sleep(2);
****Note: this is the command line:
fwrite($fp,$s_junk);
sleep(2);
socket_set_blocking($fp, 0);
****Note: this is the telnet response:
$j = fgets($fp,150);
socket_set_blocking($fp, 1);
fclose($fp);
}
?>
Now, the important thing is this. If you don't know how long the response string is, and you can't use the eof() function to learn how long it is, use the socket_set_blocking function to simply time-out the response. If your telnet connection is reasonably quick with its reply, time it out at 2 seconds. Whatever it's sending should be there by then.
k