Re: How to compile first network program? E.g. to PING google.com



linux quest wrote:
[ ... ]

Notice: steep learning curve ahead. I suggest you clarify what you want to do and what problems you are trying to solve; learning how to write simple code in C comes before learning how to write network code in C and learning how to work in Unix is a separate issue entirely.

So, my question is ...

1. How can I write a very simple C network program ... to lets say I wanted to ping google.com ???

A minimal program would involve the system() call to run the existing ping program directly:

% cat ping_google.c
#include <stdlib.h>

int main(int argc, char *argv[])
{
int ping_failed;

ping_failed = system("/sbin/ping -oq google.com > /dev/null");
if (ping_failed) {
puts("\nPinging google.com failed!\n");
} else {
puts("\nPinging google.com succeeded.\n");
}
}
% cc -o pg ping_google.c
% ./pg

Pinging google.com succeeded.

...for a more complete implementation, something which allocates its own sockets and deals with the network itself, look at /usr/src/sbin/ping/ping.c; it's about 1700 lines long. However, it would be possible to write something much smaller using libnet, for example.

2. Which directory / location in UNIX should I go to?

Most people create a location under their home directory called "Projects" or "Workareas", or something similar, and create and work on their stuff in a subdirectory under there.

3. How do I compile and execute the simple C network program - lets say doing a ping on google.com???

For trivial cases, using "cc" directly. For more complex programs, most people use Makefiles.

--
-Chuck
_______________________________________________
freebsd-questions@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: Some Advice.
    ... > int main{ ... Yes I am learning that and and implementing as I go along. ... you should provide inclusion guards for headers. ... > function definitions into separate source files. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: A problem about [Data types:string] -Need help!
    ... > int main ... find yourself learning antiquated C++ that doesn't conform to the modern ... standard. ...
    (alt.comp.lang.learn.c-cpp)
  • Newbie help with C++
    ... I have just started learning c++ and I'm having a problem with this small ... pice of code ... int main ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Visual C++ vs Visual C#
    ... idea of learning C++ without learning C to be fraught with the same ... int main ... using namespace std; ... We've added namespaces and shift operators for no gain, ...
    (microsoft.public.vc.language)
  • Re: Store a file as an integer
    ... I have recently started learning to program using Java, ... > how do I fix it? ... The error is because you're saying the int x is a FileReader object which it ...
    (comp.lang.java.programmer)