struct sockaddr_in stSockAddr; ... bind(SocketFD,(const struct sockaddr *)&stSockAddr, sizeof(struct sockaddr_in)); Why do I need to bring stSockAddr to (const struct sockaddr *)?
Sockets arranged cleverly, but beautiful. There is one basic structure and several "heirs". Their difference - they start the same, but the endings are different. It's all like with classes, only manually. Let's look at the definition of these two structures:
struct sockaddr { ushort sa_family; char sa_data[14]; }; struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; (Honestly copied from the site of Microsoft , although they could be copied in other places.)
That is, if it were on classes, then the first one was basic, and the second was an heir with two additional fields.
Now closer to the goal. Some socket functions are basic, they work with just sockets. And there are sighting - for example, working with tcp / udp sockets (there are other types of sockets - for example, file descriptors, pipes and others). The basic functions work with the base structure, sighting - with its structure. Since the beginning of the structure is the same, then the basic functions are "sideways" at the end, the main thing is to know the size (in case you need to copy, transfer somewhere further).
bind is such a basic function.
Since c ++ is a typed language, and on structures "manually inherited", it is necessary to lead "to the basic structure".
Source: https://ru.stackoverflow.com/questions/370584/
All Articles
const struct sockaddr *registered in the prototype of thebindfunction (sockaddr_inis a special case of thestruct sockaddr, look in / usr / include / ... socket.h, in.h, etc. (though there is a rather complicated architecture-dependent file structure)). - avpstruct sockaddrand nothing more than that . Actually, the memory address is passed to the function. In fact, how to interpret data (real chains of bits) depends on the function being called. On the first field,bindwill determine which of the internal implementations (IPv4, IPv6, etc.) should be called and give it your address. And already that will interpret this data as it is programmed. - avpint) to another (todouble) or long l = 123456789; int x = (unsigned char) l; // in x, the low byte of l when the size of the integer type changes, from the pointer type casts (I would call it a “fictitious” cast (as “in science” they call it - I don't remember)). In the latter case (like withbind), in fact, nothing (except for the "cheating" of the compiler) does not occur with the bit representation of the variable. - avpstruct x1, you have to pass the size of the actual structure and write the loop like this: f (struct x1 * p, size_t ps, int n) {for (int i = 0; i <n; i ++) f2 (( struct x1 *) ((char *) p + i * ps)); // instead of f2 (p [i]); } This is for passing tof2 (struct x1 *)correct address of thestruct x1to whichstruct x2. - avp