• 0

C++, how to basic program


Question

hello basicly im new to C++ i understand the very basic (al though i can not get code blocks to build and run my program...hm ) and i want to build a basic program which will ping a website (cin) and will give cout of the ipadress of what ever site was inputted... very basic... if some one could help me this is how i learn....

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Depending on your OS you'll have to look up an API for it.

What would probably be easier would be just to use some scripting language to call ping and pipe the output to a file, then parse that file to find the ip address.

C# also seems to have a useful Ping class.

Link to comment
Share on other sites

  • 0

Your request does not make sense. "Pinging" a host means sending it ICMP ECHO messages (a type of low-level internet control message) and measuring how long each request takes and how many (if any) packets are lost.

This is actually a fairly complex program that requires some knowledge about socket (internet) programming, and not just C++. Technically there's no C++ involved at all, since the socket API on virtually all operating systems are C-based (but can be called from C++).

But the reason what you say makes no sense is because you want the ping program to "cout of the ipadress of what ever site was inputted." That sounds like you really just want to resolve the hostname and not do any pinging. You say nothing about the OS, so I'm going to assume Windows. The function to do this is called GetAddrInfo, and that page also has an example (in C).

Link to comment
Share on other sites

  • 0

If you are on OSX and Linux, you can use the popen() fnction in unistd.h to use the system "ping" command and parse the output. This is the easy way. If you just and to know if the ping succeed or failed, you can use the system() function (available in all operating system in stdlib.h) and use the integer returned by the function. It is the error code. If it succeed, it will return 0, if it fail, it will return something else.

About IP, find a library for it. Probably every OS come with one.

And by the way, the linux and OSX ping command is open source, you can take a look at the code.

Link to comment
Share on other sites

  • 0

thank you all i will take everything into consideration... can some one quickly explain the basic lay out of a program source files ect.. as i read all these scripts i understand them but have no idea what theyy want me to do with them... :s put them into dif pages or what it confuses me... and in responce to i making no sense... in command prompt you can ping a website using its name (ww.blahblah.com) and it will then return that it is pinging the ipadress ... this is a common method used to find out the ipadress of a website.. and its this im targeting

Link to comment
Share on other sites

  • 0

I'm sorry, but I've explained what the ping program is and how it is not what you want. The fact that it's sometimes misused to resolve hostnames does not mean that this is what the ping program is designed to do. The ping program is designed to ping, not to resolve hostnames. Why do you want to implement a program that does something completely different from what you want?

When you just want to resolve a hostname, you use the GetAddrInfo function I linked to earlier. The manual page I linked to even has example source code, so I'm really not sure what more we can offer you.

I think you need to take a step back and find a basic C++ tutorial and start there. You can't just jump into the deep end. Once you understand the basics of C/C++, you can then start learning the sockets API. There are no shortcuts here. I could give you the full source code for a program that does what you want as it's not that complex once you understand C++ and sockets, but what would be the point of that?

Also, source code files are not "scripts," they are source code files that are passed through a compiler and linker (such as the one that comes with Visual C++) to create executable code.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.