SOCKADDR_IN sock;socksin_family = AF_INET;将字符串转换为in_addr类型socksin_addrS_unS_addr = inet_addr1921681111;socksin_port = htons5000; 将in_addr类型转换为字符串printfinet_ntoa ip = sninet_ntoasocksin_addr;解释每行代码
SOCKADDR_IN sock;- This declares a variablesockof typeSOCKADDR_IN, which is a structure defined in the Windows Socket API for storing socket address information.sock.sin_family = AF_INET;- This sets the address family of the socket toAF_INET, which is used for IPv4 addresses.sock.sin_addr.S_un.S_addr = inet_addr("192.168.1.111");- This converts the string representation of an IPv4 address "192.168.1.111" to a network byte order format and assigns it to theS_addrmember of thesin_addrstructure withinsock.sock.sin_port = htons(5000);- This converts the port number 5000 to network byte order format and assigns it to thesin_portmember ofsock.printf("inet_ntoa ip = %s\n",inet_ntoa(sock.sin_addr));- This converts thesin_addrmember ofsockfrom network byte order to a string representation of an IPv4 address using theinet_ntoafunction, and then prints it out
原文地址: https://www.cveoy.top/t/topic/iyki 著作权归作者所有。请勿转载和采集!