RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
C程序:获取本机IP地址
  • 作者:zhaozj
  • 发表时间:2020-12-23 11:03
  • 来源:未知

// 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;}