Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using fread to retrieve data from fsockopen connection... please help!

Status
Not open for further replies.

jimmyjoebob

Programmer
Apr 29, 2002
2
US
I'm having an issue that I hope someone will be able to lend a hand with. In the example below, we have a Unix server running Linux and PHP. The PHP script will attempt to open a socket communication with a Windows 2000 server using fsockopen. Basically emulating a telnet session. It will send a command to the Win2k server using fputs, then listen for the results using fread.

The problem is that it doesn't work all the time. I think it has to do with some control characters coming back that's throwing it off, but I'm not sure.

If I send: "create account (username) (password)"
The account is created!, but it will stall out on the fread!

If I send: "show account..."
It will send back the results as I need.

Doesn anyone have any feedback they'd like to offer?

Thanks, in advance, for your help!

============ START CODE SAMPLE ============
<?
function sendToHost($host,$port,$data)
{
$fp = fsockopen($host,$port,$errno,$errstr);
if (!$fp) {
return &quot;Error - $errstr ($errno)<br>\n&quot;;
} else {
fputs($fp, $data . &quot;\r\n\r\n&quot;);
$buf = fread($fp,128);
//while (!feof($fp)) {
// $buf .= fread($fp,128);
//}
fclose($fp);
return $buf;
}
}

// Set variables for communication
$host = &quot;xxx.xxx.xxx.xxx&quot;;
$port = 9999;
//$data = &quot;create account (username) (password)&quot;;
$data = &quot;show account (username)&quot;;
//
?>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
<title>Test PHP sockets</title>
</head>
<body>
This server: <b><?= $HTTP_SERVER_VARS['SERVER_NAME']; ?></b><br>
This IP: <b><?= gethostbyname($HTTP_SERVER_VARS['SERVER_NAME']); ?></b>
<hr>
Communication with <b><?= $host; ?></b> on port <b><?= $port ?></b><br>
Data sent: <b><?= $data; ?></b><br>
Data received:
<hr>
<b><?= nl2br (sendToHost($host,$port,$data)); ?></b>
</body>
</html>
============ END CODE SAMPLE ============
 
For those interested, this is what I had to do to the sentToHost() function in order to get it to work properly. (see sample below)

I'm not sure what the problem was with detecting the end-of-file, so I had to write a work-around. If ANYONE has any feedback that could help me &quot;correctly&quot; solve the problem, I'd be eternally grateful. It's not a big issue now though, since the work-around is functioning within nominal parameters. ;-)

Thanks.. and Enjoy!

============ START CODE SAMPLE ============
<?
// sendToHost : This function is used to send/receive data to a remote server
// $host : fully qualified domain name or an IP address
// $port : port on the host to communicate with
// $data : the data, or command, sent to the host
function sendToHost($host,$port,$data)
{
// Open domain socket connection
$fp = fsockopen($host,$port,$errno,$errstr);
// If the open was unsuccessful, return an error
if (!$fp) {
return &quot;Error - $errstr ($errno)<br>\n&quot;;
// If the open was successful, process the command
} else {
// Send the command to the server
fputs($fp, $data . &quot;\r\n\r\n&quot;);
// Receive the feedback from server
//
// Example data returned from the server
// -------------------------------------
// > create automated (username) (password)
// Created account 46.
// 46 13429U User4658856894
// >
// -------------------------------------
// The results will be contained between two &quot;>&quot; characters (or telnet prompts)
// For some reason, fread() isn't trapping for end-of-file! So.. we need
// to stop the feedback at the 2nd &quot;>&quot; character.
//
// $prompt_count : number of &quot;>&quot; characters encountered.
// $pos : character pointer - keeps track of buffer position.
// $char_buf : temporary buffer to hold last character received
// $all_buf : cumulative buffer of all characters received
//
// Read up to 300 characters of feedback from the server
// Stop when the 2nd &quot;>&quot; character is detected
$prompt_count = 0;
for ( $pos = 0; $pos < 300; ++$pos )
{
$char_buf = fread($fp,1);
$all_buf .= $char_buf;
if ($char_buf == &quot;>&quot;) { $prompt_count++; }
if ($prompt_count > 1) { $pos = 300; }
}
// Close domain socket connection
fclose($fp);
// Return the buffer
return $all_buf;
}
}
?>
============ END CODE SAMPLE ============
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top