*BSD News Article 81834


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!news.cs.su.oz.au!metro!metro!munnari.OZ.AU!spool.mu.edu!howland.erols.net!newsfeed.internetmci.com!mr.net!newshub.tc.umn.edu!fu-berlin.de!irz401!orion.sax.de!uriah.heep!news
From: j@uriah.heep.sax.de (J Wunsch)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: How to open a socket under FreeBSD?
Date: 27 Oct 1996 15:14:43 GMT
Organization: Private BSD site, Dresden
Lines: 27
Message-ID: <54vu93$pqf@uriah.heep.sax.de>
References: <GORSKI.96Oct26172702@axiom.www.xxx>
Reply-To: joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
NNTP-Posting-Host: localhost.heep.sax.de
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Newsreader: knews 0.9.6
X-Phone: +49-351-2012 669
X-PGP-Fingerprint: DC 47 E6 E4 FF A6 E9 8F  93 21 E0 7D F9 12 D6 4E

gorski@cips01.physik.uni-bonn.de wrote:

> I want to open a socket under FreeBSD, but all the examples I've
> found for BSD use the 'struct sockaddr_in'. FreeBSD needs 'struct
> sockaddr'! I'm not familiar with sockets. How can I open a socket
> under FreeBSD?

struct sockaddr is the generic structure to pass to the system calls.
There are several derived types from it, for the various address
families.  (OO weenies would probably call this `inheritance'.)  You
mention sockaddr_in, that's the sockaddr structure for AF_INET
objects.

The usual way is to use objects of the desired type in the code
(sockaddr_in in your case), but cast the references to (struct
sockaddr *) before passing them to the system.  Of course, unlike C++,
the inheritance is not automatic, hence you must fill in the sa_len
and sa_family values manually.  The kernel dispatches the actual
function call based on sa_family then, which in turn casts it back to
the actual structure definition.

-- 
cheers, J"org

joerg_wunsch@uriah.heep.sax.de -- http://www.sax.de/~joerg/ -- NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)