Socket Functions in PHP

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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 :.
Sending XML request...");
        socket_write( $resource, $strRequest );
        $logman->append("XML request sent. Waiting for XML
        $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

    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

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

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


Re: Socket Functions in PHP

O.J. Tibi wrote:

Quoted text here. Click to load it
Quoted text here. Click to load it

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.


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. :)

Site Timeline