banner

[Rule] Rules  [Home] Main Forum  [Portal] Portal  
[Members] Member Listing  [Statistics] Statistics  [Search] Search  [Reading Room] Reading Room 
[Register] Register  
[Login] Loginhttp  | https  ]
 
Forum Index Thảo luận hệ điều hành Windows Când giúp đỡ về Php socket  XML
  [Programming]   Când giúp đỡ về Php socket 30/09/2008 22:32:53 (+0700) | #1 | 153423
[Avatar]
pham.channhan
Member

[Minus]    0    [Plus]
Joined: 30/12/2007 23:28:22
Messages: 81
Location: Nhàcủamìnhtới
Offline
[Profile] [PM] [Yahoo!]
Code:
if($HTTP_GET_VARS['host']){$service_str=$HTTP_GET_VARS['host'];}
else{$service_str='localhost';}
if($HTTP_GET_VARS['file']){$request_str=$HTTP_GET_VARS['file'];}
 $adr = gethostbyname($service_str);echo "Ready For Create Socket<br>";
  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);echo"Create Socket Complete<br>";//socket create
  if($socket == false){$gn_sock=false;die("Fall1<br>");}              //Socket create fall
    else{$connect=socket_connect($socket,$adr,80);echo "Connecting to:<b> ".$service_str."</b><br>";   //socket connect
         if($connect==false){$gn_sock=false;die("Fall2<br>");}        //Socket connect fall
            else{$sre="GET /".$request_str." HTTP/1.1 \r\n";
                 $sre.="Connection: Keep-Alive \r\n";
                 $sre.="Host: ".$service_str." \r\n";
                 $sre.="User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1 \r\n";
                 $sre.="Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword,*/* \r\n";echo $sre;
                 socket_write($socket,$sre,Strlen($sre));echo "Sending Data Total: ".Strlen($sre)."bytes<br>";//send data
            [color=red]$gn_sock.=socket_read($socket,1024);echo $gn_sock;//recive data[/color][color=cyan]
                 socket_close($socket);}}              //close socket

Đoạn code này chạy tới dòng màu đỏ($gn_sock.=socket_read($socket,1024);echo $gn_sock;//recive data) thì ko chạy nữa, cứa đứng đó cho tới khi timeout thì thôi, dùng Localhost hay một vài host khác cũng ko chạy tiếp được, hoặc họa hiếm 'nó' mới chạy thì thông báo là Bad Request không thôi! Em là người mới học, mong mọi người chỉ giúp.
smilie
[Up] [Print Copy]
  [Question]   Re: Când giúp đỡ về Php socket 01/10/2008 03:48:40 (+0700) | #2 | 153452
anhsuytu
Member

[Minus]    0    [Plus]
Joined: 11/01/2004 03:24:46
Messages: 62
Offline
[Profile] [PM]
Các bước trước đó như create, connect thành công chứ?
[Up] [Print Copy]
  [Question]   Re: Când giúp đỡ về Php socket 01/10/2008 23:45:01 (+0700) | #3 | 153543
[Avatar]
pham.channhan
Member

[Minus]    0    [Plus]
Joined: 30/12/2007 23:28:22
Messages: 81
Location: Nhàcủamìnhtới
Offline
[Profile] [PM] [Yahoo!]
Vâng, tất cả đều thành công, rất nhanh nữa là khác( nếu bỏ bước socket_write thì tất cả ổn anh à).
[Up] [Print Copy]
  [Question]   Re: Când giúp đỡ về Php socket 05/10/2008 06:33:08 (+0700) | #4 | 153966
[Avatar]
buonviem87
Member

[Minus]    0    [Plus]
Joined: 17/02/2008 17:57:00
Messages: 14
Offline
[Profile] [PM] [WWW] [Yahoo!]
bạn dùng đoạn này để load file chứ gì?
nếu thế thì mình đã xây dựng 1 modul load file rùi nè

Code:
<FORM METHOD=post ACTION="">
©® by pntrinh<br>
<TEXTAREA NAME="listlink" ROWS="7" COLS="88"></TEXTAREA><br>
dir luu:<INPUT TYPE="text" NAME="dir">  || new name file: <INPUT TYPE="text" NAME="newtenfile">|| so part:
<INPUT TYPE="text" NAME="sf" size=3 value="1">
<INPUT TYPE="submit">
</FORM>


<?php
set_time_limit(0);
function geturl($linkf)
{
	$port=80;
	if(substr($linkf,0,7)=="http://")$linkf=substr($linkf,7);
	if(substr($linkf,0,6)=="mms://"){
		$linkf=substr($linkf,6);
		$port=1755;
	}
	$j=0;
	for($i=0;$i<strlen($linkf);$i++)
	{
		if($linkf[$i]=="/"){
			$j=$i;
			break;
		}
	}

	$host=substr($linkf,0,$j);
	$linkf=substr($linkf,$j+1);
	$j=0;
	for($i=0;$i<strlen($host);$i++)
	{
		if($host[$i]==":"){
			$j=$i;
			break;
		}
	}
	if($j>0){
		$port=substr($host,$j+1)+0;
		$host=substr($host,0,$j);
	}
	$j=0;
	for($i=strlen($linkf)-1;$i>=0;$i--)
	{
			if($linkf[$i]=="/"){
				$j=$i+1;
				break;
			}
	}
	$tenfile=substr($linkf,$j);

	$mang['host']=$host;
	$mang['port']=$port;
	$mang['linkf']=str_replace(" ","%20",$linkf);
	$tenfile=str_replace("%20","_",$tenfile);
	$mang['tenfile']=str_replace(" ","_",$tenfile);
	return $mang;
}
function createSocket($host,$port)
{
	if(strlen($_SERVER['SERVER_SOFTWARE'])<10){
			if(!$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)){echo "ko the tao socket";return false;}
			if(!socket_connect($sock, $host, $port)){echo "ko ket noi duoc!";return false;}
	}else{
			if(!$sock=fsockopen($host,$port,$errno, $errstr, 30)){echo "ko the tao socket";return false;};
	}
	return $sock;
}

function sendrequest($sock,$linkf,$host,$range,$referer,$post)
{
	$out = "GET /".$linkf." HTTP/1.1\r\n";
	$out.= "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";
	if($referer!=""){
		$out.="Referer: ".$referer."\r\n";
	}
	$out.= "Accept-Languege: en-us\r\n";
	$out.= "Accept-Encoding: gzip, deflate\r\n";
	$out.= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n";
	$out.= "Host: ".$host."\r\n";

	if($range>0){
		$out.="Range: bytes=".$range."-\r\n";
	}else {
		$out.= "Connection: Keep-Alive\r\n";
	}
	$out.="\r\n";
	if($post!="")
	{
		$out.=$post."\r\n\r\n";
	}
	flush();
	if(strlen($_SERVER['SERVER_SOFTWARE'])<10){
		if(!socket_write($sock, $out,strlen($out))){echo "ko goi request duoc.  ";return false;}
	}else {
		if(!fwrite($sock,$out)){echo "ko goi request duoc.  ";return false;}
	}
	return true;
}
function readheader($sock)
{
	if(strlen($_SERVER['SERVER_SOFTWARE'])<10){
		while($buf = @socket_read($sock,512,PHP_NORMAL_READ))
		{
			if(!trim($obuf.$buf))
			{
				@socket_read($sock,512,PHP_NORMAL_READ); // seek 1 bytes
				break; // end of header
			}
			$rbuf .= $buf; 	$obuf = $buf;
		}
	}else{
		while ($buf=fgets($sock, 512)) {
			if(!trim($buf))
			{	
				break; // end of header
			}
			$rbuf.=$buf; 
		}
	}
	return $rbuf;
}
function parse_header($buf)
{
	$lines = preg_split('/[\r\n]+/',trim($buf));
	$size = sizeof($lines);echo $size;echo "==>";
	$r['status'] = trim($lines[0]);

	if(!preg_match(';200 OK;',$lines[0]) && !preg_match(';301 Moved;',$lines[0]))
	{
		$r['error'] = TRUE;
	}
	if(preg_match(';416 Requested Range;',$lines[0]))
	{
		echo "  file loaded last time    !  ";
	}
	for($i=1; $i<$size; $i++)
	{
		$line = trim($lines[$i]);
		list($k) = preg_split('/:\s+/',$line);
		$r[strtolower($k)] = preg_replace(';^'.$k.':\s+;','',$line);
	}
	return $r;
}
function ghifile($header,$sock,$tenfile,$dirluu,$sof)
{
	$kieughi='w';
	$size=$header['content-length']+$range;
	$sf=$_POST['sf']+0;
	if($sf<=0)$sf=1;
	$sizem=$size/$sf;
	if($sizem==0)$sizem=99999999999;
	flush();
	echo "Size: ".$size."b|";
	if($sf>1)echo "size 1 part: ".$sizem."|";
	flush();
	$tendangghi=$tenfile;	
	if($sof==2)$sof=1;
	if($sof>=3){
		$sof--;
		$tendangghi.=".trinh".$sof;
	}
	$sizef=0;
	if(is_file($dirluu.$tendangghi)){
		$sizef=filesize($dirluu.$tendangghi);
		$kieughi='a';
		echo "da load: ".$range." b|";
		echo "ghi tiep....";
		flush();
	}
	if($file=fopen($dirluu.$tendangghi,$kieughi))
	{
		$header='';
		echo "writting......";
		flush();
		if(strlen($_SERVER['SERVER_SOFTWARE'])<10){
			while($buf = socket_read($sock,5024))
			{
				$sizef+=strlen($buf);
				if($sizef>$sizem){
					fclose($file);			
					$sof++;
					$tendangghi=$tenfile.".trinh".$sof;
					if($file=fopen($dirluu.$tendangghi,'w')){
						echo "<br>".$tendangghi;
					}else{
						echo "loi file";
						return false;
					}
					flush();
					$sizef=0;
				}
				if(!fwrite($file,$buf))
				{
					echo "loi file";
					return false;
				}
				$buf='';
			}
		}else {
			while(!feof($sock))
			{
				$buf = fgets($sock,5024);
				$sizef+=strlen($buf);
				if($sizef>$sizem){
					fclose($file);			
					$sof++;
					$tendangghi=$tenfile.".trinh".$sof;
					if($file=fopen($dirluu.$tendangghi,'w')){
						echo "<br>".$tendangghi;
					}else{
						echo "loi file";
						return false;
					}
					flush();
					$sizef=0;
				}				
				if(!fwrite($file,$buf))
				{
					echo "loi file";
					return false;
				}
				$buf='';
			}
		}
		fclose($file);
		
		echo "....finish!->";
		flush();
	}else {
		echo "can't create file..";
		flush();
		return false;
	}
	return true;
}
function kt_sock($sock)
{
	if(strlen($_SERVER['SERVER_SOFTWARE'])<10)socket_close($sock);
	else fclose($sock);
}
function chay($linkf,$dirluu,$dem)
{
	$mang=geturl($linkf);
	$host=$mang['host'];
	$port=$mang['port']+0;
	$linkf=$mang['linkf'];
	$tenfile=$mang['tenfile'];
	if($host!="" && $linkf!="")
	{		
		flush();
		$range=0;
		$sof=1;
		if($_POST["newtenfile"]!="" && $dem==0)$tenfile=$_POST["newtenfile"];
		$tenfile=strtolower($tenfile);
		if(is_file($dirluu.$tenfile)){
			$range=filesize($dirluu.$tenfile);
			$sof=2;
			while(is_file($dirluu.$tenfile.".trinh".$sof)){
				$range+=filesize($dirluu.$tenfile.".trinh".$sof);
				$sof++;
			}
		}
		echo "host: ".$host." ||ten file: ".$tenfile."==>";
		flush();
		// Create a new socket :
		if(($sock=createSocket($host,$port))===false)return false;
		// Send request:
		if(!sendrequest($sock,$linkf,$host,$range,"","")){
			kt_sock($sock);
			return false;
		}
		// doc header:
		$header=readheader($sock);
		$header=parse_header($header);
		if($header['location']!=""){
			kt_sock($sock);
			return $header['location'];
		}
		flush();
		
		if($header['error'] || $header['content-type']=="text/html"){
			echo "link loi break!";
			kt_sock($sock);
			return false;
		}
		flush();
		if(!ghifile($header,$sock,$tenfile,$dirluu,$sof)){
			kt_sock($sock);
			return false;
		}
		flush();
		kt_sock($sock);
		return true;
	}else return false;
}
function loclink($linkf,$dirluu,$dem)
{
	if(strpos($linkf,"rapidshare.com/files/")!=false)
	{
		$linkf=rapid($linkf,$dirluu);
	}else
	{
		$linkf=chay($linkf,$dirluu,$dem);
	}
	flush();
	if($linkf===true)return true;
	else if($linkf===false)return false;
	else {
		$kq=chay($linkf,$dirluu,$dem);
		return $kq;
	}
}

function rapid($linkf,$dirluu)
{
	$mang=geturl($linkf);
	$host=$mang['host'];
	$port=80;
	$linkf=$mang['linkf'];
	if(($sock=createSocket($host,$port))===false)return false;
	if(!sendrequest($sock,$linkf,$host,0,"","")){
			kt_sock($sock);
			return false;
		}
	// doc header:
	$header=readheader($sock);
	$header=parse_header($header);
	if($header['error']){
		kt_sock($sock);
		return false;
	}
	$dl=docbody($sock);
	$i=strpos($dl,"action=");
	$j=strpos($dl,"method=")-$i;
	$linkf=substr($dl,$i+8,$j-10);
	$linkcu=$linkf;
	$dl="";
	//--------------------------------222222
	$mang=geturl($linkf);
	$host=$mang['host'];
	$linkf=$mang['linkf'];
	if(($sock=createSocket($host,$port))===false)return false;
	$out="POST /".$linkf." HTTP/1.1\r\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\nReferer: ".$linkcu."\r\nAccept-Language: en-us\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\nHost: ".$host."\r\nContent-Length: 13\r\nConnection: Keep-Alive\r\nCache-Control: no-cache\r\n\r\ndl.start=free\r\n\r\n";

	if(strlen($_SERVER['SERVER_SOFTWARE'])<10){
		if(!socket_write($sock, $out,strlen($out))){echo "ko goi request duoc.  ";return false;}
	}else {
		if(!fwrite($sock,$out)){echo "ko goi request duoc.  ";return false;}
	}
	// doc header:
	$header=readheader($sock);
	$header=parse_header($header);
	if($header['error']){
		kt_sock($sock);
		return false;
	}
	$dl=docbody($sock);
	$i=strpos($dl,"action=");
	$j=strpos($dl,"method=")-$i;	
	$linkf=substr($dl,$i+8,$j-10);
	$linkcu=$linkf;
	$i=strpos($dl,"</table><input type=\"submit");
	$j=strpos($dl,"\"></h3></form></p>")-$i;
	$post=substr($dl,$i+56,$j-58);
//-----------------------------------------------------------33333333
	$mang=geturl($linkf);
	$host=$mang['host'];
	$linkf=$mang['linkf'];
	$tenfile=$mang['tenfile'];
	$post=str_replace(" ","+",$post);
	$post=str_replace("%28","(",$post);
	$post=str_replace("%29",")",$post);
	$post=str_replace("%23","#",$post);
	$post="mirror=on&actionstring=".$post;

	echo "host: ".$host." ||ten file: ".$tenfile."==>";
	if(($sock=createSocket($host,$port))===false)return false;
	$dai=strlen($post);
	$out="POST /".$linkf." HTTP/1.1\r\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\nReferer: ".$linkcu."\r\nAccept-Language: en-us\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Encoding: gzip, deflate\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\nHost: ".$host."\r\nContent-Length: ".$dai."\r\nConnection: Keep-Alive\r\nCache-Control: no-cache\r\n\r\n".$post."\r\n\r\n";
	
	if(strlen($_SERVER['SERVER_SOFTWARE'])<10){
		if(!socket_write($sock, $out,strlen($out))){echo "ko goi request duoc.  ";return false;}
	}else {
		if(!fwrite($sock,$out)){echo "ko goi request duoc.  ";return false;}
	}
	$header=readheader($sock);
	$header=parse_header($header);
	if($header['error'] || strpos("a".$header['content-type'],"text/html")!=false){
		echo "link loi break!";
		kt_sock($sock);
		return false;
	}
	$dl='';
	if(!ghifile($header,$sock,$tenfile,$dirluu,0)){
		kt_sock($sock);
		return false;
	}
	return true;
}

function docbody($sock)
{
	if(strlen($_SERVER['SERVER_SOFTWARE'])<10){
		while($buf = socket_read($sock,10240))
		{
			$dl.=$buf;
		}
	}else {
		while(!feof($sock))
		{
			$buf = fgets($sock,10240);
			$dl.=$buf;
		}
	}
	kt_sock($sock);
	return $dl;
}
//------------------------------------------------------------------------------------------------------------
extract($_POST);
$dirluu=$_POST["dir"];
if($dirluu!="" && !is_dir($dirluu)){
	if(mkdir($dirluu,0777))echo "<br>create new dir: '".$dirluu."' ..... ok<br>";
	flush();
}
if($dirluu!="")$dirluu.="/";
$listlink=$_POST["listlink"];
if($listlink=="")die();
$lines = preg_split('/[\r\n]+/',trim($listlink));
$size = sizeof($lines);echo $size;echo "file:<br>";
flush();
$dem=0;
for($i=0;$i<$size;$i++)
{
	$linkf=trim($lines[$i]);
	echo ($i+1).". ";
	flush();
	if(loclink($linkf,$dirluu,$dem))$dem++;
	echo "<br>";
	flush();
}
flush();
echo "<br>finish : ".$dem." file.";
echo "<br>die : ".($size-$dem)." file.End!";
flush();
?>
[Up] [Print Copy]
[digg] [delicious] [google] [yahoo] [technorati] [reddit] [stumbleupon]
Go to: 
 Users currently in here 
1 Anonymous

Powered by JForum - Extended by HVAOnline
 hvaonline.net  |  hvaforum.net  |  hvazone.net  |  hvanews.net  |  vnhacker.org
1999 - 2013 © v2012|0504|218|