chatuser.cpp

来自「利用Visual C++的Windows SOCKET网络编程实现局域网内正在使」· C++ 代码 · 共 75 行

CPP
75
字号
// ChatUser.cpp: implementation of the CChatUser class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Chat.h"
#include "ChatUser.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//重载构造函数,保存本机的主机名和IP地址
CChatUser::CChatUser()
{
	char szhostname[128];
    CString str;
	//获得主机名
	if( gethostname(szhostname, 128) == 0 )
	{
		// 获得主机ip地址
		struct hostent * phost;
		int i;
		phost = gethostbyname(szhostname);
        m_host_name=szhostname;	//保存主机名
		i=0;
		int j;
		int h_length=4;
		for( j = 0; j<h_length; j++ )
		{
			CString addr;
			
			if( j > 0 )
				str += ".";
			
			addr.Format("%u", (unsigned int)((unsigned
				char*)phost->h_addr_list[i])[j]);
			str += addr;
		}		
	}
	m_host_IP=str;	//保存IP地址
	m_pChatDlg=NULL;
}

/*CChatUser::CChatUser(CString m_host_name,CString m_host_IP)
{
	this->m_host_name=m_host_name;
	this->m_host_IP=m_host_IP;
}*/

//重载构造函数,保存其他用户的主机名和IP地址
CChatUser::CChatUser(SOCKADDR_IN addr)
{
	CString str1,str2;
	HOSTENT *pHost;
	pHost=gethostbyaddr((char*)&addr.sin_addr.S_un.S_addr,4,AF_INET);	//获取主机名
	str1.Format("%s",pHost->h_name);		//保存主机名
	str2.Format("%s",inet_ntoa(addr.sin_addr));	//保存IP地址
	m_host_name=str1;
	m_host_IP=str2;
	m_addr=addr;
	m_pChatDlg=NULL;
}

CChatUser::~CChatUser()
{

}

⌨️ 快捷键说明

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