List Info

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




How to compile first network program? E.g. to PING google.com
user name
2006-12-29 15:24:48

May I know how do I write a simple C network program in UNIX
FreeBSD? I have bought some UNIX Network Programming books,
but the problem is, the book straight away jump into some
complex program - without showing how to compile and execute
a simple program in the correct location in UNIX. I even did
some searches online, which eventually lead me to this
particular article at http://www.
faqs.org/faqs/unix-faq/socket/ - but this article still
doesn't show me how to compile a simple C program.

I know a bit about echoing a Hello World by simply typing
the following:-

abc# echo "Hello World!"
Hello World!
abc# 

So, my question is ... 

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

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

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

Thanks for any help.

Regards,
Linux Quest


 __________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection
around 
http://mail.yahoo.com 
_______________________________________________
freebsd-questionsfreebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-que
stions
To unsubscribe, send any mail to
"freebsd-questions-unsubscribefreebsd.org"
How to compile first network program? E.g. to PING google.com
user name
2006-12-29 17:19:17
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-questionsfreebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-que
stions
To unsubscribe, send any mail to
"freebsd-questions-unsubscribefreebsd.org"
How to compile first network program? E.g. to PING google.com
user name
2006-12-29 19:01:05
On Fri, 29 Dec 2006 07:24:48 -0800 (PST)
linux quest <linuxquest7570yahoo.com> wrote:

> 
> 
> May I know how do I write a simple C network program in
UNIX FreeBSD?


Have a look at this tutorial. It is pascal specific, but it
is so well
written that it will give you a good foundation.
http://ww
w.bastisoft.de/pascal/pasinet.html If you get FreePascal
(it
is a port) you will be able to follow along. The tutorial
will teach
you the basics, from there you can apply what you have
learned to any
language.
 
Bob
 
_______________________________________________
freebsd-questionsfreebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-que
stions
To unsubscribe, send any mail to
"freebsd-questions-unsubscribefreebsd.org"
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )