Connecting Tech Pros Worldwide Forums | Help | Site Map

Socket Functions in PHP

O.J. Tibi
Guest
 
Posts: n/a
#1: May 8 '06
Hi all,

I suspect I'm having problems with socket functions within my PHP
application running on Red Hat 2.4.21-4.EL #1/PHP 4.3.2/Apache 2.0.46.

Below is a sample code listing:

================================================== ==============

function sendRequest($strRequest) {
$parser = NULL;
$logman = new LogManager();

$resource = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
$logman->append("Connecting to OLS Server...");
$socket_conn = socket_connect( $resource, $serverIP, $serverPort );
if( $socket_conn ) {
$logman->append("Connected to {$serverIP}:{$serverPort}.
Sending XML request...");
socket_write( $resource, $strRequest );
$logman->append("XML request sent. Waiting for XML
response...");
$document = "";
$data = socket_read( $resource, 1024 );

while ($data != "") {
$document .= $data;
$data = socket_read( $resource, 1024 );
}
$logman->append("XML response received.");

$parser = new OlsResponseParser;
// parse the received document using our parser
$parser->parse($document);
}

if ($parser != NULL) {
return $parser->getOlsResponse();
}

return $parser;
}

================================================== ==============
The following listing is from the logs I have created...
================================================== ==============

|+-------+---------------------+------------------------------------------------------------+
| LogId | LogDate | LogMessage |
+-------+---------------------+------------------------------------------------------------+
| 37 | 2006-05-03 15:39:28 | /new_userpage_process.php initialized.
Creating buffer... |
| 38 | 2006-05-03 15:39:28 | Connecting to OLS Server...
|
| 39 | 2006-05-03 15:39:28 | Connected to (server):(port). Sending
XML request... |
| 40 | 2006-05-03 15:39:28 | XML request sent. Waiting for XML
response... |
| 41 | 2006-05-03 15:40:34 | XML response received.
|
| 42 | 2006-05-03 15:40:34 | Buffering complete. Redirecting to
thanks.php?actiontype=2 |
+-------+---------------------+------------------------------------------------------------+

================================================== ==============

As you can see, there is a lapse of more than one minutes between
LogId's 40 and 41, which is very slow on a production environment. The
script above acts as a client to another application server listening
on a remote port, running on Java. What the remote application reports
is that it receives the connection request, receives the request data,
and transmits the response data at the same second, while my
application receives the response one minute after the request was
sent!

Other implementations in the enterprise using different architectures
and technologies/languages do not experience the same issue as my app
does.

Please help. Comments, suggestions and thoughts through the group or
private mail are deeply appreciated.

Thanks!


Colin McKinnon
Guest
 
Posts: n/a
#2: May 11 '06

re: Socket Functions in PHP


O.J. Tibi wrote:

<snip>[color=blue]
> $resource = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
> $logman->append("Connecting to OLS Server...");
> $socket_conn = socket_connect( $resource, $serverIP, $serverPort );
> if( $socket_conn ) {
> $logman->append("Connected to {$serverIP}:{$serverPort}.
> Sending XML request...");
> socket_write( $resource, $strRequest );
> $logman->append("XML request sent. Waiting for XML
> response...");
> $document = "";
> $data = socket_read( $resource, 1024 );[/color]
<snip>[color=blue]
> | 40 | 2006-05-03 15:39:28 | XML request sent. Waiting for XML
> response... |
> | 41 | 2006-05-03 15:40:34 | XML response received.
> |
> | 42 | 2006-05-03 15:40:34 | Buffering complete. Redirecting to
> thanks.php?actiontype=2 |
>[/color]
+-------+---------------------+------------------------------------------------------------+


I suggest you point your script at an instance of netcat or similar and see
what's happening. I've not used the socket_ functions but perhaps its not
automatically flushing the buffer? I've not used the socket_ fns but didn't
have this problem using fsockopen().

Also, RTFM - "Note: socket_write() does not necessarily write all bytes
from the given buffer. It's valid that, depending on the network buffers
etc., only a certain amount of data, even one byte, is written though your
buffer is greater. You have to watch out so you don't unintentionally
forget to transmit the rest of your data."

It's generally bad practice not to check the return values of functions.


C.
O.J. Tibi
Guest
 
Posts: n/a
#3: May 15 '06

re: Socket Functions in PHP


Hi Colin,

I've also tried using the fsockopen() and fwrite()/fputs() combos in my
script, to no avail. The script still executes at the same rate as the
socket_x() functions that I have used above. AFAIK (correct me if I'm
wrong) fsockopen() and writing functions automatically flush the buffer
out, depending on the status of the I/O. If there are any explicit ways
of flushing the buffer, please do let me know.

I have a faint guess that the server is running a QoS (Quality of
Service) provider, the connection is being routed, or the connection is
being briged (although both the web server and the application server
reside in the same DMZ).

Thanks in advance. :)

Closed Thread