From owner-freebsd-questions@FreeBSD.ORG Fri Dec 29 17:19:27 2006 Return-Path: X-Original-To: FreeBSD-questions@FreeBSD.org Delivered-To: FreeBSD-questions@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 5902716A407 for ; Fri, 29 Dec 2006 17:19:27 +0000 (UTC) (envelope-from cswiger@mac.com) Received: from pi.codefab.com (pi.codefab.com [199.103.21.227]) by mx1.freebsd.org (Postfix) with ESMTP id 2AFD113C47E for ; Fri, 29 Dec 2006 17:19:27 +0000 (UTC) (envelope-from cswiger@mac.com) Received: from localhost (localhost [127.0.0.1]) by pi.codefab.com (Postfix) with ESMTP id 72F8E5E4F; Fri, 29 Dec 2006 12:19:26 -0500 (EST) X-Virus-Scanned: amavisd-new at codefab.com Received: from pi.codefab.com ([127.0.0.1]) by localhost (pi.codefab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vOcooR6SEh7p; Fri, 29 Dec 2006 12:19:23 -0500 (EST) Received: from [192.168.1.251] (pool-68-161-114-230.ny325.east.verizon.net [68.161.114.230]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by pi.codefab.com (Postfix) with ESMTP id 85BBA5D9B; Fri, 29 Dec 2006 12:19:23 -0500 (EST) Message-ID: <45954E15.1020708@mac.com> Date: Fri, 29 Dec 2006 12:19:17 -0500 From: Chuck Swiger User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 To: linux quest References: <978214.91301.qm@web59208.mail.re1.yahoo.com> In-Reply-To: <978214.91301.qm@web59208.mail.re1.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: FreeBSD-questions@FreeBSD.org Subject: Re: How to compile first network program? E.g. to PING google.com X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 29 Dec 2006 17:19:27 -0000 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 > > 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