Visual C++ :: Throttle Binary File Download Via WinHTTP Gives Unexpected Results
Nov 11, 2014
My Windows service may download a binary update in the background using WinHTTP APIs. I'm using code similar to the one from the bottom of this MSDN page. For ease of access I'll copy if below:
Code:
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
[Code] ....
The issue I'm experiencing is that while downloading, the code above "hogs" most of the bandwidth available on the client computer. My goal is to make it "throttle" itself on a configurable "bps" level, say at 50 kbps and not use any more bandwidth.
Someone suggested measuring my current download speed (in bps) and depending on the value make the thread "sleep" before calling WinHttpReadData again.
So the formula I'd use to calculate BPS is this:
BPS = (BytesDownloaded * 1000 * 8) / TimePassedInMilliseconds;
Then to convert to Mbps:
Mbps = BPS / (1024 * 1024);
And the waiting/sleep would be done as such:
Code:
DWORD DesiredBitsPerSec = 50 * 1000;//for 50 kbps
if(fBps > DesiredBitsPerSec) {
DWORD dwW = (DWORD)((fBps - DesiredBitsPerSec) * 1000) / DesiredBitsPerSec;
if(dwW > 0)
::Sleep(dwW);//Sleep to throttle the download speed to the one we need
}
So I implemented it into the code above but it didn't work -- the Sleep API would produce very strange delays. So I decided to add a little bit more tracking code to actually be able to see what BPS values I'm getting.
I created a small test project that is attached to this post to illustrate the issue. (It's hard to explain without running an example.)
For a test I began downloading a large binary file (Windows 10 MSI, off Microsoft's server) and at the same time had the Task Manager open on a Performance tab that was showing me the current network throughput. I made a screenshot:
But as you can see, the reading on the test program and the actual network BPS values differ greatly.
View 1 Replies
ADVERTISEMENT
Oct 31, 2014
// This is my actionlink on the index view
@Html.ActionLink("Download File", "Download", new { fileName = Model.OriginalRecordRelativeFilePath })
// This is my file download method at the controller file
public FileResult Download(string fileName)
[Code] ....
I want to get the file named path from C:DataIntergrationInterfacepath. I linked the path with file name. But i get this error System.IO.FileNotFoundException: Could not find file 'C:DataIntergrationInterfacepath when i click the actionlink.
View 4 Replies
View Related
Apr 11, 2014
I'm using Visual C++ 2010 Express on Windows 8. What I want to do at the moment, is to create a class to make WinHttp requests easier. This is the code that I'm basically using to build the class: [URL] ....
This is what I got so far. (Completely untested). Example of how I hope that it will work:
HttpRequest MyRequest;
MyRequest.Initialize("Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0","","");
htmlCode = MyRequest.SendRequest("www.cplusplus.com","GET","");
cout << htmlCode << endl;
The code:
#include "stdafx.h"
#include <windows.h>
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
class HttpRequest {
[Code] ......
View 6 Replies
View Related
Jul 8, 2013
how do i decompress an input file and write the results into an output file?
also my do while loop is supposed to keep going unless the user selects the option to exit the program, instead it exits after finishing ay of the options.
here's my code so far...
#include <iostream>
#include <iomanip>
#include <fstream>
[Code]....
View 1 Replies
View Related
Dec 28, 2013
How can I find/calculate the address of the entry point for an exe binary file using its PE32 optional header?
I have added a picture, where it says (file addr) thats the address I want to know how to calculate
View 2 Replies
View Related
Dec 6, 2013
Following is the program I wrote it basically takes 9 inputs and then save them into binary file. then print out the data stored in binary data and find inverse of it then print the inverse out. but its stuck in a loop somewhere.
Code:
#include <stdio.h>
int main() {
int a[3][3],i,j;
float determinant=0;
int x;
FILE *fp = fopen ("file.bin", "wb");
[Code] .....
View 6 Replies
View Related
Jan 18, 2015
In this one program (3 body problem), there are 6 inputs that are scanned (mass, velocity and position of the 2 bodies) and these inputs are computed to arrive at values for x, y and time for the third body. There is a working code that for of extremely small time steps, i.e over 5000 iterations, it produces values for x and y positions of the third body.
I believe this info is sent to an output file since there is a:
FILE* output=fopen("results.out", "w");
I want to play around with the code, insert different values for mass and velocity of the two bodies, but I don't know how to view the results? Im guessing I need to create a results.out file somewhere, but how do I go about doing this?
Do I need to assign memory to the results and then forward that to the output file somehow?
Here is the code : 3body.c
I have a compiler and what not, I'm just unsure how i can get these results out is all.
View 1 Replies
View Related
Apr 14, 2015
How to get this code to compile and build. It gives me a really strange error saying:
"1>Project 7 Code.obj : error LNK2019: unresolved external symbol "void __cdecl checkAnswers(char * const,char * const,int,int)" (?checkAnswers@@YAXQAD0HH@Z) referenced in function _main
1>C:UsersHaruha Raharu HarukoDesktopSpring 2015 Class WorkIntro to ProgrammingProject 7Project 7DebugProject 7.exe : fatal error LNK1120: 1 unresolved externals"
Here is the specifics on the assignment:
"Complete Programming Challenge #12 (Driver's License Exam) with file modification
The State Department of Motor Vehicles (DMV) has asked you to write a program that grades the written portion of the driver's license exam, which has 20 multiple choice questions.
Here are the correct answers:
1. B 5. C 9. C 13. D 17. C
2. D 6. A 10. D 14. A 18. B
3. A 7. B 11. B 15. D 19. D
4. A 8. A 12. C 16. C 20. A
Create a TestGrader class. The class will have an answers array of 20 characters, which holds the correct test answers. It will also have a student array of 20 characters to hold the student's answers. It will have three public member functions that enable user programs to interact with the class: setKey, setStudent, and grade. The setkey function receives a 20-character string holding the correct answers and copies this information into the answers array. The setStudent function will read in the student's answers from the file student.txt and will store the answers in a 20-character array named student. The grade method will compare each student answer to the correct answer in the key.
An applicant must correctly answer 15 or more of the 20 questions to pass the exam. After grading the exam, the program should display the following items:
-the number of right answers and the number of wrong answers
-a list of the question numbers for all incorrectly answered questions
-a message indicating whether the applicant passed or failed the exam"
View 5 Replies
View Related
Apr 9, 2013
I need to write a program that merges the numbers in two files and writes the results to a third file. The program reads input from two different files and writes the output to a third file. Each input file contains a list of integers in ascending order. After the program is executed, the output file contains the numbers from the two input files in one longer list, also in ascending order. Your program should define a function with three arguments: one for each of the two input file streams and one for the output file stream. The input files should be named numbers1.txt and numbers2.txt, and the output file should be named output.txt.
View 1 Replies
View Related
Apr 9, 2014
I am developing a program for a community launcher/downloader. What i am here to ask is there libraries to download a repo through an autoconfig file for downloading and updating the mod...
View 9 Replies
View Related
Jul 20, 2014
how to download and install gcc compiler?
View 4 Replies
View Related
Jul 11, 2014
Using the C # programming language LED controller through which controls the array is connected to the controller via the RJ45 port. I already know the basics, I mean specifics, ie
Using the instructions and DLL included with the hardware, I want to read data from it, but I can not deal with it.
Model controller is "C-Power5200" and dll is "CP5200.dll".
I would like to:-pick the data-display-edit and - save in the controller.
To read the data I am using the function
CP5200_Net_Read (BYTE * pBuf, int nSize);
and to save
CP5200_Net_Write (const BYTE * pBuf, int nLength);
View 2 Replies
View Related
Feb 2, 2015
I want to build a program that can download youtube videos, but I don't know where to start.
I can obviously use IDM to download videos, but I want to learn what goes on in the backend so I can download them manually!
I currently know python, javacript, nodejs, can I use those languages?
View 1 Replies
View Related
Mar 22, 2013
I want to create a program that lets you download torrents on linux ... how to start ...
View 1 Replies
View Related
Dec 18, 2014
Basicly I want to make a c++ program which does the following:
1- logs in to 4shared.com with username 'user_example' and password 'password_example'.
2- uploads to the 4shared account a file named 'test_upload.txt' located in 'C: est'
3- downloads a file named 'test_download.txt' from the 4sared account
I user curl 7.39.0 and curlpp 0.7.3
1- how to keep the same session between successive http requests
2- how to make an http request to upload a file
3- how to get the url of a file with specified name on a 4shared account
View 15 Replies
View Related
Jan 21, 2015
I am currently trying to understand why this example for using an array as an argument in a function has a different result than what the lecture notes say it should be.
So supposedly sum should return with the value 28, but I get 27 no matter what. I also am not very good at reading and understanding what exactly the order of operations for this function are.
Code:
#include <iostream>
using namespace std;
int sum(const int array[], const int length) {
long sum = 0;
for (int i = 0; i < length; sum += array[++i]);
return sum;
[Code] ....
View 3 Replies
View Related
Sep 14, 2013
I tried running the code below and i got an unexpected output
Code:
#include<stdio.h>
void main() {
char a='A';
while(a)
[Code] ....
The code is supossed to give an infinite loop but instead it terminates with a=0...I tried running it with some casting like this
Code:
#include<stdio.h>
void main(){
char a='A';
while((int)a)
[Code] ....
But the output was the same as before.why the code has this unexpected behaviour???
View 7 Replies
View Related
Jun 24, 2013
I am working through Binary IO with objects and am running into a curious error.
The program executes perfectly until it hits the very end. At "return 0", the program fails. The debugger picks up SIGABRT when executing "return 0". This seems to me to indicate a deconstructor problem of some kind. However, I can't seem to find any deconstructor problems (I am fairly new at programming though).
If I comment out the following two lines:
"binaryio.read(reinterpret_cast<char *>(&studentNew1), sizeof(Student));"
"binaryio.read(reinterpret_cast<char *>(&studentNew2), sizeof(Student));"
then the program finishes exiting without error . . . . but the whole point is to be able to read from the binary file. With those two lines in the code, the program successfully reads from the file and outputs the objects to the console ,but fails at "return 0";
Here is the code:
#include <iostream>
#include <fstream>
#include "Student.h"
using namespace std;
void displayStudent(Student student) {
cout << student.getFirstName() << " ";
[Code] .....
View 5 Replies
View Related
Feb 11, 2014
I have the following piece of code which should write the contents of my vetor to y file but I am getting a very weird output in my file.
for (int i=0;i<amount;i++) {
fprintf(pFileO,"Case #%d: ",i+1);
for (int j=0;j<words[i].size();j++) {
[Code].....
As you can see this doesn't make sene because the file should also contain the exact same things as the cmd outputs. What's going on here ?
View 1 Replies
View Related
Aug 7, 2013
I'm getting unexpected output in 2 different cases. The 1st deals with bitfields. The C++ standard has this line about integral promotions:
An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent all the values of the
bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field.
If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes.
This sounds like the value of a bitfield will always be treated as a signed int if the signed representation of the value will fit in the bits. This seems to hold true for my C compiler, but not my C++ compiler.
I tried storing a small negative value in a bitfield that has enough bits to store the sign bit and the value. But when I print out the bitfield, I always get a large number
In the example code below, I expect the output:
Code:
foo.x = -1
foo.y = -2
foo2.x = 31
foo2.y = 6
foo3.x = -1
foo4.x = 4294967295 But I get: Code: foo.x = 31
foo.y = 6
foo2.x = 31
foo2.y = 6
foo3.x = -1
foo4.x = -1 -------------------
The other issue I'm having is sort of similar. I'm trying to store 4294967295 into a float, but when I print it out, I get 4294967296. i've tried storing a few other large values like this and what's printed out is rarely the value I stored. I thought it might be because of some int to float conversion, so I tried 4294967295.0. Still no luck. Then I remember that defaults to a double so maybe that's the issue so I tried 4294967295.0f. Still no luck. Why can't I store the correct value here? I don't think it's an IEE format thing since I can use these values as floats on a calculator program.
The example code showing both issues is below.
Code:
#include <stdio.h>
typedef struct {
signed char x : 5;
signed char y : 3;
}my_struct_t;
[Code] .....
View 11 Replies
View Related
Jul 30, 2013
My compiler (GCC) keeps expecting an expression where it shouldn't in 1 specific piece of my code:
int zxcNewWindow( HWND parent, TCHAR *text, zxWINDOW *kid,
UINT style, int x, int y, int w, int h, int type )
// right here
{
*kid = zxDefWINDOW;
The project contains only 2 files right now and the settings are just the default for an empty Code::Blocks 12.11 project. Both files are in UTF-8 format (tried in ASCII too), I just cannot see why this is not compiling correctly. I'll post the files in the next two posts.
Edit: For those of you who didn't get what the error was from the above here's the full log:
mingw32-gcc.exe -Wall -g -DzxDEBUG -c C:MePrjscppzxGUImain.c -o objmain.o
C:MePrjscppzxGUImain.c: In function 'zxcNewWindow':
C:MePrjscppzxGUImain.c:39:10: error: expected expression before '{' token
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings (0 minutes, 0 seconds)
View 8 Replies
View Related
Jun 20, 2013
i use dev c++...i write this code to reverse an array and save the result in the same one
if n=3 i expect
a[0]=0 a[1]=1 a[2]=2 (before rev is OK but after calling rev)
a[0]=2 a[1]=1 a[2]=0 (expected result )
but i get
n=3
[code].....
View 4 Replies
View Related
May 2, 2013
I'm just starting c++, doing tutorials. So many tutorials. I've noticed that the tutorials all assume the same thing: That the user will always do exactly as he/she is told when asked for input.
Example: "Please enter your age:"
Now, the example code might be expecting the user to type some numbers, but what if the user feels like typing out the letters of their age?
"I am ninety five thank you very much, sonny"
I could specify to the user that I only want the information in number form, but what if the user is just being a dick?
What if the user types, "none o' yer business."?
So... how to approach "fool-proofing" player input?
View 2 Replies
View Related
Oct 28, 2014
I have written this regex to match a floating point literal:
(^[[:space:]]*)(([0-9]+.?[0-9]*([eE][+-]?[0-9]+)?)|"
"(.[0-9]+([eE][+-]?[0-9]+)?))([fFdD]?[[:space:]]*)$
and when I match it with string like "123e" or "e2" it works while it shouldn't and I can't find the reason why.
View 2 Replies
View Related
Oct 9, 2013
I keep getting a segmentation error when ever I have the following code...
int main(void) {
//Section 1
unsigned long val = 12;
std::vector<unsigned long> vval;
for(unsigned long i = 0; i < 100; ++i) {
vval.push_back((unsigned long)0);
[Code] ....
Error: *** Error in `/home/alex/projects/bignum/build/bignum': free(): invalid pointer: 0x00007ffff75b5b88 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7ffff7274a46]
compiler is clang++ 3.2
It doesn't happen if I restructure it so that bignum::num is not a pointer to an std::vector<unsigned long>
View 10 Replies
View Related
Apr 1, 2014
I have a WordRecord that contains a LinkedList (both my doing). I have rigorously tested my LinkedList class, and know that it works (heck, I used it in the last project I had!). The problem is that undefined behaviour seems to happen when using the WordRecord, which has a std::string and a LinkedList<unsigned>. (The problem happens with the LinkedList.)
Here is the code:
main.cpp
#include <iostream>
#include "BinaryTreeNode.h" // here for test purposes
#include "LexicographicTree.h"
#include "LinkedList.h"//also for test purposes
#include "OutputStream.h"
#include "WordRecord.h"
using namespace std;
int main()
{
// setup the OutputStream to print to "test.txt"
OutputStream stream("test.txt");
// create a sampleWordRecord (make it have the word "I" on line 1)
[code]....
One of the requirements for the project is that it must compile on Unix server (I am using Windows, and have tested it in both environments.) I get a core-dump in the Unix environment. On the other hand, the output on-screen in the Windows environment looks right. However, when I open up the text file, I get the following
Sample word record:
WordLines
I{14}
/* I have no what is happening to sampleWordRecord's LinkedList; I am not trying to modify it, except for where I created the sampleWordRecord! */
View 14 Replies
View Related