jimmyjoebob
Programmer
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 "Error - $errstr ($errno)<br>\n";
} else {
fputs($fp, $data . "\r\n\r\n"
$buf = fread($fp,128);
//while (!feof($fp)) {
// $buf .= fread($fp,128);
//}
fclose($fp);
return $buf;
}
}
// Set variables for communication
$host = "xxx.xxx.xxx.xxx";
$port = 9999;
//$data = "create account (username) (password)";
$data = "show account (username)";
//
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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 ============
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 "Error - $errstr ($errno)<br>\n";
} else {
fputs($fp, $data . "\r\n\r\n"
$buf = fread($fp,128);
//while (!feof($fp)) {
// $buf .= fread($fp,128);
//}
fclose($fp);
return $buf;
}
}
// Set variables for communication
$host = "xxx.xxx.xxx.xxx";
$port = 9999;
//$data = "create account (username) (password)";
$data = "show account (username)";
//
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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 ============