Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 12-05-2003, 09:27 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
file() and setting a timeout

i have a site where i pull a file in remotely from another url. for example:
PHP Code:
<?
$array 
file("http://somehost/somefile.txt");

foreach(
$array as $each)
{
  echo 
"$each <br>\n";
}
?>
sometimes it is possible that this remote file will not be available. if it is not available, the code above will cause the page to hang, and then print an error.

i want it to spend 1 second max checking for this file, .. and if it doesn't find it, then print "not available"

what would be the best way to go about this? maybe using curl? is there any other way?
__________________
Mike
sde is offline   Reply With Quote
Old 12-16-2003, 07:18 PM   #2 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
I've looked at this various times in the past, but never came up with anything. Let me know if you find something that works.

-r
idx is offline   Reply With Quote
Old 12-16-2003, 10:31 PM   #3 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
well, now you're getting into deep networking land, and probably a bit out of the scope of php's file implementation.

you would probably want to use non-blocking sockets and implement the http transaction yourself. seems that php's socket functions are pretty young at the moment, so it might be safer to write an external program that will accomplish this for you. if you want to stick to php, here's an example someone posted on the php.net socket_connect() comment section (although the right way to do this would have been to use select() instead of continuous connecting).

PHP Code:
<?
   $host 
"127.0.0.1";
   
$port "80";
   
$timeout 15;  //timeout in seconds

   
$socket socket_create(AF_INETSOCK_STREAMSOL_TCP)
     or die(
"Unable to create socket\n");

   
socket_set_nonblock($socket)
     or die(
"Unable to set nonblock on socket\n");

   
$time time();
   while (!@
socket_connect($socket$host$port))
   {
     
$err socket_last_error($socket);
     if (
$err == 115 || $err == 114)
     {
       if ((
time() - $time) >= $timeout)
       {
         
socket_close($socket);
         die(
"Connection timed out.\n");
       }
       
sleep(1);
       continue;
     }
     die(
socket_strerror($err) . "\n");
   }

   
socket_set_block($this->socket)
     or die(
"Unable to set block on socket\n");
?>
joe_bruin is offline   Reply With Quote
Old 12-17-2003, 08:29 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
thanks joe. for my application sockets turned out to be the best way to go.

i was running a few tests with php sockets a few months back, and everything seems to be ok if the server you are calling is linux. there seems to be a bug with php socket functions and windows though.

i ended up using a socket script for this one, but in an indirect way. the server i was reading the file from is a game server, and i have other scripts which connect and check game status. if the server doesn't respond, then i just don't try to read the file.

basically the same thing you are showing here.

another option is curl too: http://www.php.net/manual/en/function.curl-setopt.php .. you must have the curl extention installed correctly though. it can be a pain, especially if you are developing or hosting on windows. if curl is installed and running correctly on the server, you can use the set_opt TIMEOUT to read a file. curl is great! i use it for sending and receiving xml securely with ups.
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 09:50 AM


All times are GMT -8. The time now is 08:57 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting