CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2018
    Posts
    158

    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; 
    };

  2. #2
    Join Date
    Nov 2018
    Posts
    121

    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.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: structure sockaddr_in

    Quote Originally Posted by zio_mangrovia View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured