What is and what is the sockaddr_in structure for?

Asked

Viewed 842 times

4

Lately I’ve been wanting to program a network sockets in C++ but as the matters on the internet about it are a little limited coming to show only how to program the sockets without explaining in more detail the use of each function I ended up getting lost a little and one of the things that left me in doubt was the use of the structure "sockaddr_in". Therefore, what is and what is the sockaddr_in structure for?

1 answer

4


In short, it is this structure that deals with network addresses.

struct sockaddr_in {
    short            sin_family;
    unsigned short   sin_port;
    struct in_addr   sin_addr;
    char             sin_zero[8];
};

This particular one is the structure used with addresses Ipv4, for example 192.168.123.123.

In addition to this structure there are several other basic structures for all systems and functions that deal with Internet addresses. Many times you will use getaddrinfo () to fill these structures and then read them when necessary.

  • 1

    "This in particular is the structure used with Ipv4 addresses..." - What would be the structure to work with IPV6?

  • @Khyser The structure to Ipv6 is the struct sockadd_in6

Browser other questions tagged

You are not signed in. Login or sign up in order to post.