-
April 12th, 2023, 12:11 PM
#1
structure sockaddr_in
Code:
#include <sys/socket.h>
#include <netinet/in.h>
struct sockaddr_in {
sa_family_t sin_family;
in_port_t sin_port; /* network order */
struct in_addr sin_addr;
};
struct in_addr {
uint32_t s_addr; /* network order */
};
Why It has been declared the structure in_addr ?
I could to declare the structure sockaddr_in as this one ?
Code:
struct sockaddr_in {
sa_family_t sin_family;
in_port_t sin_port; /* network order */
uint32_t s_addr;
};
-
April 13th, 2023, 05:25 AM
#2
Re: structure sockaddr_in
1. _in isn't the only kind of address: See also https://man.archlinux.org/man/core/m...kaddr.3type.en
2. On your system for today, it happens to be a uint32. This won't be necessarily true for all systems across all of time.
3. The structure serves to document it's purpose. A bare uint32 could be anything.
4. If it became necessary to change what an 'in_addr' was, the structure means it's only changed in one place. Having just a bare uint32 is a massive search/replace with scope for lots of errors.
-
April 13th, 2023, 05:38 AM
#3
Re: structure sockaddr_in
 Originally Posted by zio_mangrovia
Code:
...
struct in_addr {
uint32_t s_addr; /* network order */
};
Why It has been declared the structure in_addr ?
...
In addition to what salem_c already wrote I'd recommend to look at its definition in Microsoft winsock2.h file (in_addr structure (winsock2.h)):
Code:
struct in_addr {
union {
struct {
u_char s_b1;
u_char s_b2;
u_char s_b3;
u_char s_b4;
} S_un_b;
struct {
u_short s_w1;
u_short s_w2;
} S_un_w;
u_long S_addr;
} S_un;
};
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|