[ciapug] TCP devel

Ryan O'Rourke ciapug@cialug.org
13 Jan 2004 13:02:45 -0600


First off, I'd like to introduce myself since I'm new to this list -
although I probably know many of you from such lists as cialug or
amesfug :-)
I'm a network and sysadmin for a small retail chain in Ames. I don't do
any development yet, but I try to pick things up and try to help our
developer as much as possible. Our entire operation is in the process of
making the transition from MS to Linux and Open Source. This creates
some, shall we call them, "learning opportunities" for our developer as
he is very new to such concepts as Perl, PHP, MySQL, etc.
I appreciate your help as we all learn! Please bear with me as I try to
explain our problems in layman's terms.
Oh, and one other note, if any of you ever see any code that creates a
security concern or is just plain stupid please speak up! You won't
offend anyone here!

Anyway, we're trying to do some credit authorization type transactions
where we send data across a TCP port, wait for a reply, process that
reply, and continue the communication back and forth. 
Apparently, we can send data, but when the response comes back our
script hangs and the socket does not close.
We're using:
RedHat AS 2.1
PHP 4.1.2-2.1.6 (newest version available in RPM format for RH EL)
Apache 1.3.23-10 

The code:
$length = strlen($output);
$hexlength = dechex($length);
$hexlength1 = "\x95";
$header = "\x02\x46\x44\x02\x00$hexlength1";
$footer = "\x03\x46\x44\x03";
$header .= "$output$footer";
$output = "$header";


$host = "206.201.XXX.XXX";
$port = XXXXX;

//Open a client connection
$fp = fsockopen($host, $port, $errno, $errstr);

if (!$fp){
$result = "Error: Could not open socket connection";
}else{
//Write the user string to the socket
fputs ($fp, $output, strlen($output));
//get the result
while(!eof($fp))
{
$result .= fgets($fp, 1024);
}
//close the connection
fclose($fp);
}


I should also add that our developer is pushing me to upgrade to PHP 4.3
since it supports streams - http://us2.php.net/manual/en/ref.stream.php
He thinks no support for streams is the root of the problem here. I
don't want to upgrade to PHP 4.3 since that will mean using an
unsupported version on a box where we're paying $1,500/yr for support.

Thanks in advance!

-- Ryan