CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2009
    Posts
    3

    Throttlng file upload speed

    Hey all,
    I'm trying to upload files to a server but I would like to limit the maximum upload speed that it sends the files at. the reasoning is some people who will use this program have awful upload speeds and when uploading the files, it really lags them. so i would like to limit the maximum upload speed if at all possible. here is the code that is currently doing the uploading. yes i know it is very basic, its more dynamic in the code but this is the basics of it. if anyone could point me in the right direction, i would greatly appreciate it.


    WebClient myWebClient = new WebClient();
    myWebClient.UploadFile(""http://myurl", "POST", "C:\\windows\\test.jpg");

  2. #2
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Throttlng file upload speed

    try to use Sleep() to delay the process of uploading... for use a for loop for some bigger count so that time is eaten....

  3. #3
    Join Date
    Sep 2009
    Posts
    3

    Re: Throttlng file upload speed

    well i dont want to delay it because i would still encounter the same problem. every time it uploaded a chunk, it would do it at max speed it possibly could. i dont want that. i want to completely throttle it down to do like 5KB/s maximum. if i just sleep it, i will still get lag spikes and i cant have that.

  4. #4
    Join Date
    Jul 2006
    Posts
    297

    Re: Throttlng file upload speed

    In its simplest sense all throttling is really doing is sending X number of bytes at a time then waiting X amount of time to ask for more data. This can be achieved by overloading the stream class, however, you may have to use something other than a WebClient to achieve throttling.

    Check out this url http://www.codeproject.com/KB/IP/Ban...hrottling.aspx

  5. #5
    Join Date
    Sep 2009
    Posts
    3

    Re: Throttlng file upload speed

    yeah i stumbled upon that earlier today and im probably going to end up going that way. but the problem is what im sending it to. im sending it to a php page and specifying variables in the url for php to use. cause i need to specify which person the file is from and where the destination on the server is. so with that, i'm totally clueless as to how to use this method to do it because that would require me to use sockets, which would make it hard to specify any extra info other than just the file. mainly because i dont know *** i'm doing with php. and i cant develop a c# server for this either because the server is on a linux box.

  6. #6
    Join Date
    Jul 2006
    Posts
    297

    Re: Throttlng file upload speed

    Well you can probably use HttpWebRequest to transfer what you need to or you can use sockets to send it. I remember awhile back I had to use sockets to post something with PHP all you need to do is construct the header correctly and its really just as simple as sending a stream of bytes through the socket to the right url / port.

    Not sure if it helps but here was the php code i used maybe you can convert it to C#

    Code:
    	
    	$url = preg_replace("@^http://@i", "", $url);
    	$host = substr($url, 0, strpos($url, "/"));
    	$uri = strstr($url, "/");
    	
    	$post = "";
    	foreach($data as $key=>$val) {
    		if(!empty($post)) $post .= "&";
    		$post .= $key . "=" . url_encode($val);
    	}
    	
    	$length = strlen($post);
    	$header =   
    		"POST $uri HTTP/1.1\r\n" .
    		"Host: $host\r\n" . "User-Agent: syncDatabase\r\n".
    		"Content-Type: application/x-www-form-urlencoded; charset:UFT-8\r\n".
    		"Content-Length: $length\r\n".
    		"Connection: Close\r\n\r\n".
    		"$post\r\n";
    	
    	$socket = @ fsockopen("http://www." . $host, 80, $errno, $errstr, 10);
    	if($socket != false)
    	{
    		$str = "";
    		fputs($socket, $header);
    		while (!feof($socket)) {
    			$str .= fread($socket, 128);
    		}
    		fclose($socket);
    		
    		// Parse the response, and return the header + content
    		$str = explode("\n\r", $str);
    		$str[0] = explode("\r", $str[0]);
    		
    		$header = "";
    		foreach($str[0] as $var)
    		{
    			if(preg_match("/(?<setting>.+): (?<value>.+)/", $var, $matches))
    				$header[$matches["setting"]] = $matches["value"];
    		}
    		
    		$response["header"] = $header;
    		$response["content"] = trim($str[1]);	
    		return $response;
    	}
    	else
    	{
    		return '';
    	}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured