Simple Winsock routine

zhaozj2021-02-17  56

The functionality of this program is used to get the host name (HostName) and IP address.

The procedure is as follows:

#include #include

INT DOIT (int RGC, Char ** RGV) {Char AC [80]; Struct Hostent * Phe; struct in_addr addr; INT i; if (getHostName (AC, SIZEOF (AC)) == Socket_ERROR) {Printf ("Error % D when getting local hostname./n ", wsagetlasterror ()); return 1;} Printf (" The Host Name IS:% S / N ", AC); PHE = gethostByName (AC); if (PHE == 0 {Printf ("Error! Bad host lookup./N"); return 1;} for (i = 0; Phe-> h_addr_list [i]! = 0; i) {Memcpy (& addr, Phe-> h_addr_list [I], SIZEOF (STRUCT IN_ADDR)); Printf ("Address% D IS% s. / N", I, INET_NTOA (ADDR));} return 0;} int main (int Argc, char * argv []) {WSADATA WSADATA; INT RETVAL; IF (WsaStartup (MakeWord (1, 1), & WSADATA)! = 0) {Return 255;} RetVal = DOIT (Argc, Argv); wsacleanup (); Return RetVal;

转载请注明原文地址:https://www.9cbs.com/read-30952.html

New Post(0)