Re: how to use: struct sockaddr
Re: how to use: struct sockaddr
- Subject: Re: how to use: struct sockaddr
- From: Dakidd <email@hidden>
- Date: Thu, 16 Dec 2004 14:37:54 -0800
>From: Ryan M Joseph <email@hidden>
>Subject: Re: how to use: struct sockaddr
>
>I have a simple question that has me very confused. I'm trying to use
>CFSocket to set up a client/server over the internet but I can not
>understand how the sockaddr struct can be used to describe and IP
>address. Is is not intended to do this? do I need to use some other
>form of address then an IP? Or is there a function I can not find which
>maybe builds the struct for you based off an IP string? Thanks for your
>help.
Note: Everything I'm about to say assumes you're working with IPv4 - the
"conventional" method of internet addressing, where an IP address is a
string of four numbers, 0..255, separated by dots - IE, 129.18.204.71 - or
a hostname "host.name.net", which resolves to the IP numbers in question.
Things are a bit different when messing around with IPv6 (the "new and
improved" method of addressing), and I know so little about working with
IPv6 that I won't even TRY to touch on it beyond mentioning that it exists
and is different.
A sockaddr_in (which is what I'm assuming you actually meant when you said
"struct sockaddr") struct has three fields:
sin_family (an unsigned 8 bit int)
sin_addr (an unsigned 32 bit int)
sin_port (an unsigned 16 bit int)
For purposes of illustration, let's assume you're trying to talk to a
server living on "example.machine.net" that listens on port 234. Further,
let's assume that "example.machine.net" resolves to the dotted-decimal IP
string "1.2.3.4" when passed into a DNS query.
For IPv4 internet addressing, the sin_family field is always set to
AF_INET. (which one header file or another that you use should be
#define-ing, enum-ing, or otherwise setting up to be the decimal value 42)
Stuff VarName.sin_family, which you've previously declared via a line
similar to
struct sockaddr VarName;
with either AF_INET or 42, whichever is more convenient/readable for you.
The sin_addr field is set to the 32 bit value of the address thatyou want
to talk to. Do a normal DNS query by your favorite method to get the
dotted-decimal IP string that corresponds to "example.machine.net" - In
this example, "1.2.3.4". Now convert the dotted-decimal IP string into a 32
bit value, either by "losing" the dots, and converting each of the four
numbers into an 8-bit value, then cramming them together, either into a
UInt32 variable that you can then assign directly to VarName.sin_addr, or
by doing the conversion directly into VarName.sin_addr. This can be done
manually, or by using a helper routine to do the conversion for you.
Whichever way you do it, VarName.sin_addr ends up holding the value
0x01020304. (Decimal 16909060)
The sin_port field is set to the 16 bit port number (in network byte order)
of the host, whose address you specified in sin_addr, that you wish to
speak to. Since we know that you want to talk to port number 234, and we
know that Macs speak "network byte order" natively, that means you want
VarName.sin_port to hold the value 0x00EA, which is the same as decimal 234.
>From there, it's just a matter of passing VarName (or a pointer to it, if
that's what the routine you're calling requires) to whatever routine you're
using that takes a struct sockaddr_in as input.
Don Bruder - email@hidden <--- Preferred Email - unmunged
I will choose a path that's clear: I will choose Free Will! - N. Peart
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden