Cast struct in struct [duplicate]

-5

I'm using sockets on windows

and when I get to the connect function

I have the following parameter:

(struct sockaddr *) & server (which is my socket)

How does this cast work?

    
asked by anonymous 29.08.2015 / 23:14

1 answer

1

The short answer is that socket functions expect a sockaddr structure, but you are typically passing a sockaddr_in, sockaddr_un, or other structure.

Socket methods work for a number of network technologies (Internet, IPv6 Internet, Ethernet, Unix Sockets) and each technology has a different structure to describe a network address.

The sockaddr structure "generic" only the member sa_family, whose value identifies the type of address, and 14 more bytes of filler that will be used by the specialized structures. For example, the sockaddr_in structure has only 7 usable bytes: sa_family = AF_INET, port and IP address.

This is because C language does not have the concept of classes and inheritance. If it had, if it were C ++, sockaddr_in could be a subclass of sockaddr, and the functions would accept sockaddr_in without the cast.

    
30.08.2015 / 06:06