sockets (long)
| From: | Joseph Blythe | Date: | Thu, 05 Apr 2001 04:29:13 +0000 |
| Subject: | sockets (long) | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-47166@lists.php.net to get a copy of this message | ||
Hey all,
People who have been recieving this list for a while may remember me going through all this a while back, so I am not really a newbie when using sockets. I am basically trying to rewrite my Online Credit Card Verification (OCV) client with the new socket functions.
In thoery the following should work:
--- example1 socket functions ----
$socket = socket(AF_INET, SOCK_STREAM, 0);
$conn = connect($socket, some_ip_address, some_port);
set_nonblock($socket);
$packet = "a request";
write($socket, $packet, some_length);
$start = time();
$buffer = "";
while ( empty($buffer) && (time() < $start + 30 )) {
read($socket, $buffer, some_length);
}
This does not work will hang until the script times out (max execution time is exceeded).
----- exampe 2 network and file system functions -----
$socket = fsockopen (some_address, some_port, $errno, $errstr, 30);
set_socket_blocking($socket, 0);
$packet = "a request";
fputs ($socket, $packet);
$start = time();
while ( empty($buffer) && (time() < $start + 30 )) {
$buffer = fgets($socket, some_length);
}
Works! After 30 seconds fgets is timed out.
The lengths I am reading and writing are correct as all request and response lengths are pre-determined.
I cannot use set_socket_blocking($socket, 0) in example 1 instead of set_nonblock($socket) as it will generate an errora s follows:
Warning: Supplied argument is not a valid File-Handle resource ...
I believe that in example 1 the socket is not being set to nonblock mode?
Can anyone verfiy this, give me a better way, or maybe just put me out of my misery :-)
Regards,
Joseph