Return-Path: william@bourbon.usc.edu Delivery-Date: Fri Aug 29 19:20:57 2008 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on merlot.usc.edu X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.3 Received: from bourbon.usc.edu (bourbon.usc.edu [128.125.9.75]) by merlot.usc.edu (8.14.1/8.14.1) with ESMTP id m7U2Kv6k028692 for ; Fri, 29 Aug 2008 19:20:57 -0700 Received: from bourbon.usc.edu (localhost.localdomain [127.0.0.1]) by bourbon.usc.edu (8.14.2/8.14.1) with ESMTP id m7U2S08C029244 for ; Fri, 29 Aug 2008 19:28:00 -0700 Message-Id: <200808300228.m7U2S08C029244@bourbon.usc.edu> To: cs551@merlot.usc.edu Subject: Re: Regarding CS-551 Warmup Project-1 Date: Fri, 29 Aug 2008 19:28:00 -0700 From: Bill Cheng Someone wrote: > Lecture 2 slide 14 uses the following line to set the server's address in > the client code. > serv_addr.sin_addr.s_addr = inet_addr("nunki.usc.edu"); > > When I do this my connect fails and gives me the following error "Network is > unreachable". It works if I replace "nunki.usc.edu" with the IP address of > the server. > Since the input to the commandline has to be hostname, I am using > gethostbyname() function. > > What might be the reason that the code given in the slides is not working? Hmm... I don't have Steven's book with me. I guess I must have copied something incorrectly. The man pages of inet_addr() says: The inet_addr() and inet_network() functions interpret char- acter strings that represent numbers expressed in the IPv4 standard `.' notation, returning numbers suitable for use as IPv4 addresses and IPv4 network numbers, respectively. The inet_makeaddr() function uses an IPv4 network number and a local network address to construct an IPv4 address. The inet_netof() and inet_lnaof() functions break apart IPv4 host addresses, then return the network number and local network address, respectively. So, it should be used if the argument passed to it is a string that has dotted numbers, e.g., "68.181.201.3". So, you should only call inet_addr() if the first character in a hostname is between '0' and '9'. Otherwise, you should probably call gethostbyname(). Sorry about the bug in the slides. I'll fix it when I have the book in front of me. -- Bill Cheng // bill.cheng@usc.edu