Page 1 of 1

Multiple socket problem

Posted: Tue Jul 24, 2007 9:22 am
by rishi
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,
I am trying to open multiple sockets and wants to use them simultaneously for fetching webpages over the net.
say i m having "n" number of url's, so  i am opening n sockets for it and after opening it i am fetching web contents simultaneously for all.

problem i am facing is that its not accepting url's other then domain or index page.
eg:- Its accepting "www.example.com"   but not accepting "www.example.com/abc/xyz" etc etc...

Code: Select all

$hosts = array("www.example1.com","www.example2.com");
	$timeout = 30;
	$status = array();
	$sockets = array();
  foreach ($hosts as $id => $host) {
		$s = stream_socket_client("$host:80", $errno, $errstr, $timeout, 
		STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
		if ($s) {
			$sockets[$id] = $s;
			$status[$id] = "in progress";
		
		} else {
			$status[$id] = "failed, $errno $errstr";
		}
this is my code for opening sockets, if any one have solution, then kindly suggest me....

[s]thanx[/s] [thanks in advance.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]11.[/b] Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.[/quote]

Posted: Tue Jul 24, 2007 9:34 am
by TheMoose
It won't accept "www.example.com/abc/xyz" because those are folders on the targetted machine, not part of the server's address to connect to.

http://us.php.net/stream_socket_client

Code: Select all

$s = stream_socket_client("$host:80", $errno, $errstr, $timeout,
STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
fwrite($s, "GET /abc/xyz HTTP/1.0\r\nHost: http://www.example.com\r\nAccept: */*\r\n\r\n");
while (!feof($s)) {
    echo fgets($s, 1024);
}
fclose($s);
Notice the part above where it specifies which folder/file to get (GET /abc/xyz).

Posted: Wed Jul 25, 2007 1:41 am
by rishi
Thanks for reply it works ,
its getting to the targeted folder but if i wants to get particular file then its not opening.
eg:- its opening

Code: Select all

fwrite($s, "GET /abc/xyz HTTP/1.0\r\nHost: http://www.example.com\r\nAccept: */*\r\n\r\n");
but its not opening particular file..

Code: Select all

fwrite($s, "GET /abc/xyz.html HTTP/1.0\r\nHost: http://www.example.com\r\nAccept: */*\r\n\r\n");
is there any way to solve it?

thanks again.

Posted: Wed Jul 25, 2007 2:50 am
by volka
Do you get any response from the server?

Posted: Wed Jul 25, 2007 4:39 am
by rishi
No, there is no response from server side.

Posted: Wed Jul 25, 2007 4:48 am
by volka
And no other error/Warning message at all?
Strange,

Code: Select all

<?php
$host = 'de2.php.net';

$s = stream_socket_client("$host:80");
fwrite($s, "GET /manual/en/function.socket-create.php HTTP/1.0\r\nHost: $host\r\nAccept: */*\r\n\r\n");
while (!feof($s)) {
    echo fgets($s, 1024);
}
fclose($s);
works fine for me.

Posted: Wed Jul 25, 2007 8:41 am
by rishi
yaa, its working fine now.

:D
thank you

Posted: Sat Jul 28, 2007 12:58 am
by rishi
I am using fwrite() function to fetch web page contents, but it is not taking more then 8K data at a time.
I am using PHP 5.
Following is my code for getting page contents.

Code: Select all

$hosts = array("example1.com","example2.com");

foreach ($hosts as $id => $host) {
		$s = stream_socket_client("$host:80", $errno, $errstr, $timeout, 
		STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
	
		if ($s) {
			$sockets[$id] = $s;
			$status[$id] = "in progress";
		
		} else {
			$status[$id] = "failed, $errno $errstr";
		}
	}

  while (count($sockets)) {
		$read = $write = $sockets;
		$n = stream_select($read, $write, $e = null, $timeout);
		
		if ($n > 0) {
			foreach ($read as $r) {
				$id = array_search($r, $sockets);
				$data = fread($r,8194);					
						
				if (strlen($data) == 0) {
				if ($status[$id] == "in progress") {
					$status[$id] = "failed to connect";
				}
				fclose($r);
				unset($sockets[$id]);
				} else {
					$status[$id] .= $data;
				}
		  }
here i have opened "n" no. of sockets for "n" no. of URL's , they are in array $sockets.
I want to fetch page content of all the pages simultaneously.