www.pudn.com > ChatRoom.zip > ClientSelectDlg.cpp, change:2011-04-25,size:8048b


// ClientSelectDlg.cpp : 实现文件 
// 
 
#include "stdafx.h" 
#include "ClientSelect.h" 
#include "ClientSelectDlg.h" 
 
#define  MAX_BUF_SIZE 1024 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#endif 
 
 
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// 对话框数据 
	enum { IDD = IDD_ABOUTBOX }; 
 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持 
 
// 实现 
protected: 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
END_MESSAGE_MAP() 
 
 
// CClientSelectDlg 对话框 
 
 
 
 
CClientSelectDlg::CClientSelectDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CClientSelectDlg::IDD, pParent) 
{ 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	m_bInitWinSock = FALSE; 
	m_hEndTalkingEvent = NULL; 
	m_hStopWorkingEvent = NULL; 
	m_pThreadTalking = NULL; 
	m_socketServer = NULL; 
} 
 
void CClientSelectDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	DDX_Control(pDX, IDC_LIST1, m_lstRecvMsg); 
	DDX_Control(pDX, IDC_EDIT1, m_edtSendMsg); 
} 
 
BEGIN_MESSAGE_MAP(CClientSelectDlg, CDialog) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	//}}AFX_MSG_MAP 
	ON_WM_DESTROY() 
	ON_BN_CLICKED(IDC_BUT_CONNECT, &CClientSelectDlg::OnButConnect) 
	ON_BN_CLICKED(IDC_BUT_SEND, &CClientSelectDlg::OnBnClickedButSend) 
END_MESSAGE_MAP() 
 
 
// CClientSelectDlg 消息处理程序 
 
BOOL CClientSelectDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// 将“关于...”菜单项添加到系统菜单中。 
 
	// IDM_ABOUTBOX 必须在系统命令范围内。 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 
	//  执行此操作 
	SetIcon(m_hIcon, TRUE);			// 设置大图标 
	SetIcon(m_hIcon, FALSE);		// 设置小图标 
 
	// TODO: 在此添加额外的初始化代码 
	m_bInitWinSock = InitWinSock(); 
 
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE 
} 
 
void CClientSelectDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// 如果向对话框添加最小化按钮,则需要下面的代码 
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序, 
//  这将由框架自动完成。 
 
void CClientSelectDlg::OnPaint() 
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // 用于绘制的设备上下文 
 
		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); 
 
		// 使图标在工作矩形中居中 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// 绘制图标 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
//当用户拖动最小化窗口时系统调用此函数取得光标显示。 
// 
HCURSOR CClientSelectDlg::OnQueryDragIcon() 
{ 
	return static_cast<HCURSOR>(m_hIcon); 
} 
 
 
BOOL CClientSelectDlg::InitWinSock() 
{ 
	WSADATA wsaData; 
	int nResult = WSAStartup(MAKEWORD(2,2),&wsaData); 
	if (nResult!=NO_ERROR) 
	{ 
		m_lstRecvMsg.AddString(_T("WSAStartup调用失败")); 
		WSACleanup(); 
		return FALSE; 
	} 
 
	if (LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=2) 
	{ 
		WSACleanup(); 
		return FALSE; 
	} 
 
	return TRUE; 
} 
 
//select模型 
BOOL CClientSelectDlg::socketSelect(SOCKET hSocket,int nTimeOut /* = 100 */,BOOL bRead /* = TRUE */) 
{ 
	//1.建立一个集合,将套接字加入到集合中 
	fd_set fsetArray; 
	//2.将集合清空 
	FD_ZERO(&fsetArray); 
	//3.把传进来的套接字加入到集合中 
	FD_SET(hSocket,&fsetArray); 
	//4.用select这个函数来判断当前是否可读或可写 
	int nRet; 
	timeval tvTimeOut; 
	nTimeOut = nTimeOut>1000?1000:nTimeOut; 
	tvTimeOut.tv_sec = 0; 
	tvTimeOut.tv_usec = nTimeOut; 
	//如果是读 
	if (bRead) 
	{ 
		nRet = select(0,&fsetArray,NULL,NULL,&tvTimeOut); 
	} 
 
	else 
	{ 
		nRet = select(0,NULL,&fsetArray,NULL,&tvTimeOut); 
	} 
 
	//判断一下内核返回的结果 
	if (nRet<=0) 
	{ 
		return FALSE; 
	} 
 
	else 
	{ 
		//判断一下传进来的套接字还在不在集合里 
		if (FD_ISSET(hSocket,&fsetArray)) 
		{ 
			return TRUE; 
		} 
		else 
		{ 
			return FALSE; 
		} 
	} 
 
} 
 
 
void CClientSelectDlg::CloseWinSock() 
{ 
	if (m_hEndTalkingEvent) 
	{ 
		CloseHandle(m_hEndTalkingEvent); 
		m_hEndTalkingEvent = NULL; 
 
	} 
 
	if (m_hStopWorkingEvent) 
	{ 
		CloseHandle(m_hStopWorkingEvent); 
		m_hStopWorkingEvent = NULL; 
	} 
 
	if (m_bInitWinSock) 
	{ 
		WSACleanup(); 
	} 
 
	if (m_socketServer) 
	{ 
		closesocket(m_socketServer); 
		m_socketServer = NULL; 
	} 
 
} 
 
void CClientSelectDlg::Close() 
{ 
	SetEvent(m_hEndTalkingEvent); 
	if (m_pThreadTalking) 
	{ 
 
		if (WaitForSingleObject(m_hStopWorkingEvent,1000)!=WAIT_OBJECT_0) 
		{ 
			TerminateThread(m_pThreadTalking,-1); 
			delete m_pThreadTalking; 
			m_pThreadTalking = NULL; 
		} 
	} 
	CloseWinSock(); 
 
} 
void CClientSelectDlg::OnDestroy() 
{ 
	CDialog::OnDestroy(); 
 
	// TODO: 在此处添加消息处理程序代码 
	Close(); 
} 
 
 
 
void CClientSelectDlg::OnButConnect() 
{ 
	// TODO: 在此添加控件通知处理程序代码 
	//判断库是否加载成功 
	if (!m_bInitWinSock) 
	{ 
		m_lstRecvMsg.AddString(_T("未加载库,不能启动服务器")); 
		return ; 
	} 
 
	//创建事件 
	m_hEndTalkingEvent = CreateEvent(NULL,TRUE,FALSE,NULL); 
	if (INVALID_HANDLE_VALUE==m_hEndTalkingEvent) 
	{ 
		m_lstRecvMsg.AddString(_T("创建EndTalkingEvent失败")); 
		CloseWinSock(); 
	} 
 
	m_hStopWorkingEvent = CreateEvent(NULL,TRUE,FALSE,NULL); 
	if (INVALID_HANDLE_VALUE==m_hStopWorkingEvent) 
	{ 
		m_lstRecvMsg.AddString(_T("创建StopWorkingEvent失败")); 
		CloseWinSock(); 
	} 
 
	//创建线程 
	m_pThreadTalking = AfxBeginThread(ThreadTalking,this,0,0,CREATE_SUSPENDED,NULL); 
	if (!m_pThreadTalking) 
	{ 
		CloseWinSock(); 
	} 
 
	//让线程跑起来 
	m_pThreadTalking->ResumeThread(); 
} 
 
UINT CClientSelectDlg::ThreadTalking(LPVOID lParam) 
{ 
	CClientSelectDlg *pThis = (CClientSelectDlg*)lParam; 
	//1.创建一个套接字 
	pThis->m_socketServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 
	//2.连接 
	sockaddr_in sockAddrServer; 
	sockAddrServer.sin_family = AF_INET; 
	sockAddrServer.sin_addr.s_addr = inet_addr("127.0.0.1"); 
	sockAddrServer.sin_port = htons(0x9527); 
 
	if (SOCKET_ERROR == connect(pThis->m_socketServer,(SOCKADDR*)&sockAddrServer,sizeof(sockAddrServer))) 
	{ 
		pThis->m_lstRecvMsg.AddString(_T("连接服务器错误")); 
		pThis->CloseWinSock(); 
		pThis->m_pThreadTalking = NULL; 
		return -1; 
	} 
 
	pThis->m_lstRecvMsg.AddString(_T("连接服务器成功")); 
 
	int nRecvNum = 0; 
	TCHAR szRecvBuf[MAX_BUF_SIZE]; 
 
	while(1) 
	{ 
		if (WaitForSingleObject(pThis->m_hEndTalkingEvent,0)==WAIT_OBJECT_0) 
		{ 
			break; 
		} 
		if (pThis->socketSelect(pThis->m_socketServer,100,TRUE)) 
		{ 
			//清空 
			memset(szRecvBuf,0,sizeof(szRecvBuf)); 
			nRecvNum = recv(pThis->m_socketServer,(char*)szRecvBuf,sizeof(szRecvBuf),0); 
			if (nRecvNum>0) 
			{ 
				pThis->m_lstRecvMsg.AddString(szRecvBuf); 
			} 
 
			else 
			{ 
				pThis->m_lstRecvMsg.AddString(_T("服务器已关闭")); 
				break; 
			} 
		} 
 
		Sleep(10); 
	} 
 
	SetEvent(pThis->m_hStopWorkingEvent); 
	return 1; 
 
} 
void CClientSelectDlg::OnBnClickedButSend() 
{ 
	// TODO: 在此添加控件通知处理程序代码 
	//1.得到Edit里的内容,判断 
	CString strValue; 
	m_edtSendMsg.GetWindowText(strValue); 
	//2.socketselect,判断客户端是否处于可写状态 
	if (!strValue.IsEmpty() && socketSelect(m_socketServer,100,FALSE)) 
	{ 
		send(m_socketServer,(char*)strValue.GetBuffer(),strValue.GetLength()*sizeof(TCHAR),0); 
		//清空 
		m_edtSendMsg.SetWindowText(_T("")); 
	} 
}