C언어,ARM

PC의 ip얻기

안녕1999 2020. 3. 21. 23:00

 

char *GetMyIPstr(int i,char *buf)
{
	char name[128];struct hostent *phe;

	*buf=0;
	if(gethostname(name,sizeof(name))==0)
	{
		phe=gethostbyname(name);
		if(phe!=NULL)
		{
			struct in_addr *pin;int j;
			for(j=0;j<=i;j++)
			{
				pin=(struct in_addr *)phe->h_addr_list[j];
				if(pin==0)break;
				if(i==j)
				{
					strcpy(buf,inet_ntoa(*pin));
					return buf;
				}
			}
		}
	}
	else
	{
		int err=GetLastError();//10093 WSANOTINITIALISED
		if(err==10093)
		{
			WSAStartup();
			return GetMyIPstr(i,buf);
		}
	}
	return 0;
}
wchar_t *GetMyIPstr(int i,wchar_t *buf)
{
	char b[32];
	GetMyIPstr(i,b);
	strcpy(buf,b);
	return buf;
}