473,394 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Socket Functions in PHP

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!

May 8 '06 #1
2 1634
O.J. Tibi wrote:

<snip>
$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 ); <snip> | 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 |

+-------+---------------------+------------------------------------------------------------+
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.
May 11 '06 #2
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. :)

May 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Martin | last post by:
I am a PHP newbie (just got my "Hello World" page working this morning). I'm doing some R&D work to see if PHP is viable for a situation I have. To accomplish what I want to do, I have to have the...
1
by: Andrew Pasetti | last post by:
Using PHP Version 4.3.7/Apache/2.0.46 (Red Hat), I am not given access to php's socket functions. However, when I run the socket program by serving it as a web page, everything works as expected. ...
4
by: Bryan Olson | last post by:
Here's the problem: Suppose we use: import socket f = some_socket.makefile() Then: f.read() is efficient, but verbose, and incorrect (or at least does not play will with others);
11
by: Mike M | last post by:
Is it possible? In the parent process, I have a socket that binds, listens and then accepts new connections (which creates new sockets in the process). I want to be able to pass some of these new...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
2
by: Nuno Magalhaes | last post by:
I've got a simple problem I guess. How do I know when a connection is terminated without losing any data? I do something like the code below, but sometimes between socket.Receive and socket.Send...
6
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I've developed a program that uses a socket to receive information 24h a ~ day. The problem is that the socket seems to freeze. By that I...
1
by: Ismail Demiralp | last post by:
Hi, I want to open a permanent socket connection to a service witch is running on the same computer. After I logged in on my php page, I start a session. After this a socket connection should...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.