www.gusucode.com > 基于VC++的局域网视频聊天系统源码程序 > 基于VC++的局域网视频聊天系统源码程序/code/ChatServer/ChatServerDlg.cpp
// ChatServerDlg.cpp : implementation file // #include "stdafx.h" #include "ChatServer.h" #include "ChatServerDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include "SocketClient.h" ///////////////////////////////////////////////////////////////////////////// // CChatServerDlg dialog CChatServerDlg::CChatServerDlg(CWnd* pParent /*=NULL*/) : CDialog(CChatServerDlg::IDD, pParent) { //{{AFX_DATA_INIT(CChatServerDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CChatServerDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CChatServerDlg) DDX_Control(pDX, IDC_MESSAGEBOX, m_clMessageBox); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CChatServerDlg, CDialog) //{{AFX_MSG_MAP(CChatServerDlg) ON_BN_CLICKED(IDC_BT_START, OnBtStart) ON_BN_CLICKED(IDC_BT_STOP, OnBtStop) ON_BN_CLICKED(IDC_BT_EXIT, OnBtExit) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CChatServerDlg message handlers BOOL CChatServerDlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here init(); return TRUE; // return TRUE unless you set the focus to a control } // windows 消息响应函数 BOOL CChatServerDlg::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if( pMsg->message==WM_KEYDOWN ) { switch(pMsg->wParam) { case VK_RETURN: return true; case VK_ESCAPE: return true; } } return CDialog::PreTranslateMessage(pMsg); } void CChatServerDlg::OnBtStart() { // TODO: Add your control notification handler code here SetMessageBox("服务器初始化..."); //文本 m_Server = new CSocketServer(); //为服务器套接字分配空间 SetMessageBox("OK!\r\n启动服务器..."); //文本 m_Server->Init( this ); SetMessageBox("Succuss!\r\n"); //其它功能变化 m_bRun = true; //服务器启动标志 GetDlgItem(IDC_BT_START)->EnableWindow(FALSE); //运行不可用 GetDlgItem(IDC_BT_STOP)->EnableWindow(TRUE); //停止钮可用 } void CChatServerDlg::OnBtStop() { // TODO: Add your control notification handler code here SetMessageBox("正在停止服务器...."); if ( m_Server) delete m_Server; //释放监听套接字资源 POSITION pos; for( pos=m_connectionList.GetHeadPosition() ; pos != NULL ; ) { CSocketClient * t= (CSocketClient *)m_connectionList.GetNext(pos); delete t; } m_connectionList.RemoveAll();//释放响应套接字资源 SetMessageBox("OK!\r\n"); m_bRun = FALSE; //服务器停止标志 GetDlgItem(IDC_BT_START)->EnableWindow(TRUE); //运行可用 GetDlgItem(IDC_BT_STOP)->EnableWindow(FALSE); //停止钮不可用 SetMessageBox("服务器已经停止!\r\n"); } void CChatServerDlg::OnBtExit() { // TODO: Add your control notification handler code here EndDialog(1); } //文本显示 void CChatServerDlg::SetMessageBox(CString mes) { m_clMessageBox.SetSel(m_clMessageBox.GetTextLength(), m_clMessageBox.GetTextLength()+1); m_clMessageBox.ReplaceSel(mes); m_clMessageBox.LineScroll(1); } //接收客户端套接字 void CChatServerDlg::PocessAcceptEvent() { CSocketClient * m_cClieSocket = new CSocketClient(); if ( m_Server->Accept( * m_cClieSocket) ) //接受一个套接字 { if ( !m_bRun) return ;//服务器是否启动 m_cClieSocket->Init(this); } else delete m_cClieSocket; } //设置在线人数 void CChatServerDlg::SetOnlineNum(int UsersNum) { CString szNickName ; if ( UsersNum == 0) m_iUserNum = 0; else m_iUserNum += UsersNum; szNickName.Format("用户(%d人)",m_iUserNum); GetDlgItem(IDC_ONLINE_USERLIST)->SetWindowText(szNickName); } //界面初始化 void CChatServerDlg::init() { BYTE IP[4]; //数组装32位IP地址 struct hostent * shHostent; //主机信息结构体 char szHostName[128]; //主机名 /*对界面IP地址控件和端口控件初始化*/ gethostname(szHostName,128); // 获得主机名 /*if语句由主机名得主机信息结构体,从而对获得32位IP地址*/ if( (shHostent = gethostbyname(szHostName) ) != NULL) //由主机名得主机信息 { //得到主机信息后对IP地址赋值 IP[0] = ((struct in_addr *)shHostent->h_addr_list[0])->S_un.S_un_b.s_b1; IP[1] = ((struct in_addr *)shHostent->h_addr_list[0])->S_un.S_un_b.s_b2; IP[2] = ((struct in_addr *)shHostent->h_addr_list[0])->S_un.S_un_b.s_b3; IP[3] = ((struct in_addr *)shHostent->h_addr_list[0])->S_un.S_un_b.s_b4; } else //得主机信息失败返回false return ; //对IP地址控件赋值 ((CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS))->SetAddress(IP[0],IP[1],IP[2],IP[3]); ((CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS))->EnableWindow(false);//禁用IP地址控件 ((CEdit *)GetDlgItem(IDC_PORT))->SetWindowText("8123");//对端口赋值 //其它设置 m_bRun = false; m_iUserNum = 0; //在线用户数为0; SetOnlineNum(0); //设置在线人数 GetDlgItem(IDC_BT_STOP)->EnableWindow(FALSE); //停止钮不可用 } ////////////////////////////////////////////////////////////////////////// //增加用户到用户列表控件 void CChatServerDlg::AddUserToList(CString szID) { ((CListBox *)GetDlgItem(IDC_USERLIST))->AddString(szID); } //删除用户从用户列表控件 void CChatServerDlg::DelUserFromList(CString szNickName) { int index; index = ((CListBox *)GetDlgItem(IDC_USERLIST))->FindString(0,szNickName); ((CListBox *)GetDlgItem(IDC_USERLIST))->DeleteString(index); } ////////////////////////////////////////////////////////////////////////// //用户连接后通过了服务器验证,加入到用户链表 void CChatServerDlg::AddUserToChain(CSocketClient *ClientSocket) { m_connectionList.AddTail(ClientSocket); } //用户退出后从用户链表中删除 void CChatServerDlg::DelUserFromChain(CString szNickName) { POSITION pos,t_pos; for( pos=m_connectionList.GetHeadPosition() ; (t_pos = pos) != NULL ; ) { CSocketClient * t= ( CSocketClient *)m_connectionList.GetNext(pos); if ( t->m_szID == szNickName ) { m_connectionList.RemoveAt(t_pos); } } } //得到用户链表中的第一个用户 POSITION CChatServerDlg::GetFirstUserFromCHain() { return m_connectionList.GetHeadPosition(); } ////////////////////////////////////////////////////////////////////////// //保存用户ID和IP的相关信息到链表 void CChatServerDlg::AddUserInfoToChain(CString ID, CString IP) { USERINFO * newUser = new USERINFO; newUser->ID = ID; newUser->IP = IP; m_userInfo.AddTail(newUser); } //从链表中删除用户ID和IP的相关信息 void CChatServerDlg::DelUserInfoFromChain(CString ID) { POSITION pos,t_pos; for( pos=m_userInfo.GetHeadPosition() ; (t_pos = pos) != NULL ; ) { USERINFO * t= (USERINFO *)m_userInfo.GetNext(pos); if ( t->ID == ID ) { m_userInfo.RemoveAt(t_pos); } } } //拷贝用户IP和IP信息 void CChatServerDlg::UserInfoCopy( CString &str) { str = _T(""); POSITION pos; for( pos=m_userInfo.GetHeadPosition() ; pos != NULL ; ) { USERINFO * t= (USERINFO *)m_userInfo.GetNext(pos); str += t->ID ; str += " "; str += t->IP; str += " "; } }