C Program: Get this unit IP address

xiaoxiao2021-03-06  99

// Louis 2004-7-27

#include "stdafx.h"

INT _TMAIN (int Argc, _tchar * argv []) {int Ret; char buf [1024]; Word WVersionRequested; Wsadata Wsadata; int Err;

Struct Hostent * Host;

// Socket Initialize WVersionRequested = MakeWord (2, 2); Err = WSAStartup (WVersionRequested, & Wsadata); if (Err! = 0) {Return -1;}

MEMSET (BUF, 0, 1024);

// Get hostname ret = gethostname (BUF, 1024); if (RET! = 0) {Printf ("Return IS% D / N", RET); RET = WsageTlasterror (); Printf ("Specific Error IS% D / N ", RET); Printf (" gethostname error./n ");} else printf (" Hostname IS% S / N ", BUF);

// Get Host IP Address Host = gethostByName (BUF); if (Host == Null) {PERROR ("gethostByname"); return -1;} else // printf ("IP Address IS% S / N", INET_NTOA * (Host-> h_addr_list))))))))))))); Printf ("IP Address IS% S / N", INET_NTOA (* (IN_ADDR *) Host-> h_addr_list [0])));

Return 0;}

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

New Post(0)