Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > socket_read can not read the whole HTTP page?
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 3rd November 18:14
kd
External User
 
Posts: 1
Default socket_read can not read the whole HTTP page?



Blocked on "while ($out = socket_read($socket, 1024)) ".
browser show the processbar all the time , and the page is not completed
display,

If I press ESC key to cancel the request , the resource of page show that :
the target URL were not read completed .
next codes never been executed .

Why socket_read blocked?
Additional : The network and URL are absolute valid all the time . the size
of target page is about 30k bits .

script :
<?
header("Content-type: text/html; charset=UTF-8"); error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>\n<pre>";

$service_port = 80;
$host = "10.1.1.144";
$file = "/index.aspx";
$address = gethostbyname('10.1.1.144');

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0)
{
echo "socket_create() failed.\n reason: " . socket_strerror($socket) .
"\n";
} else
{
echo "OK.\n";
}

echo "try connect to '$address' : '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result < 0)
{
echo "socket_connect() failed.\n reason: ($result) " .
socket_strerror($result) . "\n";
} else {
echo "OK.\n";
}

//$in = "HEAD / HTTP/1.1\r\n";
$in = '';

$in .= "GET {$file} HTTP/1.1\r\n";
$in .= "Accept: text/html\r\n";
$in .= "Accept-Language: zh-cn\r\n";
//$in .= "Accept-Encoding: gzip, deflate\r\n";
$in .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET
CLR 2.0.50727)\r\n";
$in .= "Host: {$host}\r\n";
$in .= "Cache-Control: no-cache\r\n";
$in .= "Connection: Keep-Alive\r\n\r\n";

echo "send HTTP HEAD request...\n{$in}";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
echo "read response:---------------\n\n<textarea>";
$len = 0;
$out= '';
while ($out = socket_read($socket, 1024)) //
-------------------------------wait for too long time .!!!!!!!!!
{
$len += strlen($out);
echo $out; }
echo "</textarea>";


echo "close socket ...";
socket_close($socket);
echo "OK.\n\n";
?>
  Reply With Quote


 


2 3rd November 18:15
eddie
External User
 
Posts: 1
Default socket_read can not read the whole HTTP page?



try to change this to $in .= "GET {$file} HTTP/1.0\r\n";

and change this to
$in .= "Connection: closed\r\n\r\n";

--
Eddie Dunckley - eddie@rttc.co.za - Realtime Travel Connections
IBE Development, www.rttc.co.za, cell 083-379-6891, fax 086-617-7831
Where 33deg53'37.23"S 18deg37'57.87"E Cape Town Bellville Oakdale ZA
Chaos, panic, and disorder - my work here is done.
  Reply With Quote
3 3rd November 18:16
eddie
External User
 
Posts: 1
Default socket_read can not read the whole HTTP page?


soz that should be close not closed;

--
Eddie - Chaos, panic, and disorder - my work here is done.
  Reply With Quote
4 3rd November 18:16
kd
External User
 
Posts: 1
Default socket_read can not read the whole HTTP page?


Wow, That's correct answer! Thank to Eddie Dunckley !

scheme:
$in .= "GET {$file} HTTP/1.0\r\n"; //-----------
$in .= "Accept: text/html\r\n";
$in .= "Accept-Language: zh-cn\r\n";
$in .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET
CLR 2.0.50727)\r\n";
$in .= "Host: {$host}\r\n";
$in .= "Cache-Control: no-cache\r\n";
$in .= "Connection: closed\r\n\r\n";//------------must be "closed"

1.must be "HTTP/1.0" : why? IE post "HTTP/1.1" .
2. must be "closed" : so that should be close not closed;

Both modification are required.

Shall anyone tell me sth. about "HTTP/1.0" and "HTTP/1.1" ?

best regards!
ked
  Reply With Quote


 


5 4th November 09:57
ceo
External User
 
Posts: 1
Default socket_read can not read the whole HTTP page?


HTTP 1.1 requires more complex client/server interaction, and if your
client (which is about as brain-dead simple as it gets) isn't doing
that complex stuff, 1.1 will not work.

All HTTP 1.1 servers will work just fine with 1.0, so stick with that
unless you really NEED a feature of 1.1

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?
  Reply With Quote
Reply


Thread Tools
Display Modes




666