Cast struct em struct

Asked

Viewed 94 times

-5

Guys I’m using sockets in windows

and when I get to connect

I have the sequinte parameter:

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

how this cast works?

  • 7

    Do not create multiple questions, you can edit your another question and give the details there.

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 numerous network technologies (Internet, Internet Ipv6, Ethernet, Unix Sockets) and each technology has a different structure to describe a network address.

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

This is because the 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.

Browser other questions tagged

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