#if !defined(AFX_SOCKINFO_H__FA14210C_2BED_11D6_81EB_0080C8ED46AF__INCLUDED_)
#define AFX_SOCKINFO_H__FA14210C_2BED_11D6_81EB_0080C8ED46AF__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "afxcmn.h"
#include "afxwin.h"
class CSockInfo
{
public:
static CString GetLocalHostIP();
static CString GetLocalHostName();
CSockInfo();
virtual ~CSockInfo();
};
#endif // !defined(AFX_SOCKINFO_H__FA14210C_2BED_11D6_81EB_0080C8ED46AF__INCLUDED_)
#include "stdafx.h"
#include "CSockInfo.h"
#include <afxsock.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSockInfo::CSockInfo()
{
}
CSockInfo::~CSockInfo()
{
}
CString CSockInfo::GetLocalHostName()
{
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
memset(name,'\0',255);
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname (name, sizeof(name)) == 0)
{
return name;
}
WSACleanup( );
}
return "";
}
CString CSockInfo::GetLocalHostIP()
{
CString strIP("");
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
memset(name,'\0',255);
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
strIP= inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}
WSACleanup( );
}
return strIP;
}