*BSD News Article 81730


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!munnari.OZ.AU!news.Hawaii.Edu!news.uoregon.edu!news-peer.gsl.net!news.gsl.net!howland.erols.net!newsfeed.internetmci.com!uunet!in3.uu.net!newsflash.concordia.ca!sunqbc.risq.net!news1.sympatico.ca!news.bellglobal.com!rrwood
From: Roy Wood <rrwood@mars.execulink.com>
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: How to open a socket under FreeBSD?
Date: Mon, 28 Oct 96 09:53:24 -0500
Organization: Silicon Angst Software
Lines: 60
Distribution: world
Message-ID: <961028094417-rrwood@mars.execulink.com>
References: <GORSKI.96Oct26172702@axiom.www.xxx>
Reply-To: rrwood@mars.execulink.com
NNTP-Posting-Host: ppp5.saturn.execulink.com
X-To: gorski@cips01.physik.uni-bonn.de
X-Newsreader: rnMac v2.0d9

In article <GORSKI.96Oct26172702@axiom.www.xxx>, 
gorski@cips01.physik.uni-bonn.de writes:

> 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?
> I've seen something like  connect(sock, (struct sockaddr *) &name, sizeof(name))
> in some source codes. Is that the way? This seems to be wrong for me.

It's fine.  Since sockets can be of many types (Internet, unix, file 
system, etc.), the 'struct sockaddr' is a general structure (check the 
header files if you're really curious about the details).  In your case, 
since you're dealing with only an Internet socket, you use a 'struct 
sockaddr_in', which is defined with the correct structure elements for 
an Internet address.  The trailing '_in' on the 'sockaddr_in' should 
help you remember that this is an Internet-related data structure.

Anyway, use the 'sockaddr_in' structure and just typecast it when you 
bind the address to the socket.

>  * In the included file <netinet/in.h> a sockaddr_in is defined as follows:
>  * struct sockaddr_in {
>  *   short     sin_family;
>  *   u_short   sin_port;
>  *   struct in_addr sin_addr;
>  *   char sin_zero[8];
>  * };

Yeah-- there's the sockaddr_in definition for you.


>      struct sockaddr_in name;
>
>      /* Create name with wildcards. */
>      name.sin_family = AF_INET;
>      name.sin_addr.s_addr = INADDR_ANY;
>      name.sin_port = 0;
>      if (bind(sock, &name, sizeof(name))) {
>           perror("binding datagram socket");
>           exit(1);
>      }

And this should work quite nicely as an example, though you'll probably 
want to typecast that pointer to 'name'.  Try this:

if (bind(sock,(struct sockaddr *) &name, sizeof(name))) {
           perror("binding datagram socket");
           exit(1);
      }


By the way, the programmer's supplementary documents for 4.4BSD have a 
couple of good tutorials about all of this.  O'Reilly publishes the 
whole 4.4BSD documentation series in a nice format, though I'm sure you 
can download them somewhere too.

-Roy

--
http://www.mars.execulink.com/~rrwood