clocalmachine.cpp

来自「Mpeg流的网络客户端实时播放」· C++ 代码 · 共 84 行

CPP
84
字号
// CLocalMachine.cpp: implementation of the CLocalMachine class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CLocalMachine.h"

//#include <Winsock2.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//#pragma comment(lib, "Ws2_32.lib")
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLocalMachine::CLocalMachine()
{
	m_bInit = false;
	InitSystem();
}

CLocalMachine::~CLocalMachine()
{
	Uninit();
}

void CLocalMachine::InitSystem(void)
{
	if (m_bInit)
		return;

	// Init socket
	WORD     wVersionRequested = MAKEWORD(2, 0);
	WSADATA  wsaData;
	if (WSAStartup(wVersionRequested, &wsaData) == 0)
	{
		m_bInit = true;
	}

	// Other init...
	if (m_bInit)
	{
		//m_bInit = 
	}
}

void CLocalMachine::Uninit(void)
{
	// Clean up socket
	if (m_bInit)
	{
		WSACleanup();
	}
}

bool CLocalMachine::GetIPAddress(char * outIP, char * outHost)
{
	if (m_bInit)
	{
		char   name[255];
		if (gethostname(name, sizeof(name)) == 0)
		{
			// Output the host name
			if (outHost != NULL)
				strcpy(outHost, name);

			PHOSTENT  hostinfo;
			if((hostinfo = gethostbyname(name)) != NULL)
			{
				// Output local machine's IP address string
				LPCSTR pIP = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
				if (outIP != NULL)
					strcpy(outIP, pIP);
				return true;
			}
		}
	}
	return false;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?