因为sendto的参数要的就是
(struct sockaddr *)
类型的参数,这个结构可以支持多种地址类型,典型的例子就是ipv4和ipv6。
但是通常你的接受端的地址是通过地址解析得到的,sockaddr_in(对应ipv4)或者是sockaddr_in6(对应Ipv6),而不是struct sockaddr,所以需要类型转换。
至于如何区分指向的到底是什么类型的地址,主要依靠第一个字段ushort sa_family;来区分。
MSDN里面的描述是:
Winsock functions using sockaddr are not strictly interpreted to be pointers
to a sockaddr structure. The structure is interpreted differently in the context
of different address families. The only requirements are that the first
u_short is the address family and the total size of the memory buffer in
bytes is namelen.