Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4002454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:01:52+00:00 2026-05-20T08:01:52+00:00

I have a C++ console app that uses wininet.h to go out to a

  • 0

I have a C++ console app that uses wininet.h to go out to a URL, and download the contents of a web page.

The contents are usually just a single IP address. It goes here: http://www.whatismyip.com/automation/n09230945.asp

Everything works great.

Then I decided to create my own IP checker in PHP, using the following code:

<?php

$ip = $_SERVER['REMOTE_ADDR'];

header("Cache-Control: private");
header("Content-Type: text/html");

echo $ip;

?>

This looks correct in the browser, identical whatismyip.com’s results, but the C++ program just adds bunch of junk after the IP, then repeats the IP half cut off, and then adds more junk.

What is causing this? I tried analyzing the headers, but I can’t spot the difference.

Also, I tried putting a plain txt file on to the server, and the C++ program reads it perfect.

I also tried changing my headers to both plain/text and text/plain. Same result.

Thank you for your help!

Edit: Here’s a portion of the C++ code:

HINTERNET OpenAddress = InternetOpenUrl(connect,"http://www...", NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_KEEP_CONNECTION, 0);

char DataReceived[16] = " ";
DWORD NumberOfBytesRead = 0;
while (InternetReadFile(OpenAddress, DataReceived, 16, &NumberOfBytesRead) &&     NumberOfBytesRead)
{
     cout << DataReceived;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:01:52+00:00Added an answer on May 20, 2026 at 8:01 am

    cout will expect a string to be null-terminated. Because you’re just reading bytes into a buffer, and not null-terminating them at the end, cout will carry on past the end of the bytes you’ve read and just carry on dumping memory out until it hits a null pointer or memory protection kicks in.

    With your code, what’s happening is this, at a guess:

    1. You assign a 16-byte area of memory.
    2. You call InternetReadFile. This puts an IP address, say “127.0.0.1” in your buffer, without a null terminator.
    3. You call cout with DataReceived. This is a char array, and cout therefore expects it to be a null-terminated string. It outputs every character from the start of the buffer, right past “127.0.01” and onwards until it finds a 0 in memory.
    4. Because “127.0.0.1” was all there is to read, and your buffer was bigger than that, the next call to InternetReadFile leaves NumberOfBytesRead as zero, so your loop only happens once.

    Don’t know anything about InternetReadFile(), but I’d guess an approach like this should work if you’re only grabbing a single line with an IP address in it:

    char DataReceived[64]; // I guess I'm antsy about having plenty of room
    
    DWORD NumberOfBytesRead = 0;
    if (InternetReadFile(OpenAddress, DataReceived, 63, &NumberOfBytesRead)) {
      DataReceived[NumberOfBytesRead] = '\0';
      cout << DataReceived;
    } else // handle error condition
    

    But fundamentally, I think the main problem you’re having is in confusing a buffer that’s just a bunch of bytes with a nice friendly null-terminated string, and you should understand that, and maybe look for some existing examples of using InternetReadFile with that in mind to see how they work, and therefore what you need to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little web app that uses the Facebook like widget. This page
I have a console app built on .NET 4 that uses the HttpClient library
I have a console app that performs a lengthy process. I am printing out
I have a console application that uses an app.config file however I can't figure
We have an in house developed web-based admin console that uses a combination of
We have a simple console app that uses EF 4 to process data; read
I have a web app that uses Spring's Log4jConfigurer class to initialize my Log4J
I have a console app that uses 20 or so threads to connect to
Newbie question... I have a working console app that uses a shared unit I
I have a largish web app that uses a Javascript inheritance style that duplicates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.