聊天軟件代碼_第1頁(yè)
聊天軟件代碼_第2頁(yè)
聊天軟件代碼_第3頁(yè)
聊天軟件代碼_第4頁(yè)
聊天軟件代碼_第5頁(yè)
已閱讀5頁(yè),還剩15頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、I服務(wù)器:/ ServerDlg.cpp : implementation file/#include stdafx.h#include Server.h#include ServerDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();protected:DECLARE_MESSAGE_MA

2、P();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)void CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)END_MESSAGE_MAP()/ CServerDlg dialogCServerDlg:CServerDlg(CWnd* pParent /*=NULL*/): CDialog(CServerDlg:IDD, pParent)m_hIcon = AfxGetApp(

3、)-LoadIcon(IDI_ICONAPP);void CServerDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);DDX_Control(pDX, IDC_LIST_USER, m_lbUser);DDX_Text(pDX, IDC_EDIT_DISPLAY, m_strDisplay);BEGIN_MESSAGE_MAP(CServerDlg, CDialog)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BN

4、_START, OnBnStart)ON_BN_CLICKED(IDC_BN_CLOSE, OnBnClose)ON_MESSAGE(WM_RECEIVE, OnReceive)ON_MESSAGE(WM_CLIENTCLOSE, OnClientClose)ON_MESSAGE(WM_ACCEPT, OnAccept)ON_WM_DESTROY()END_MESSAGE_MAP()/ CServerDlg message handlersBOOL CServerDlg:OnInitDialog()CDialog:OnInitDialog();ASSERT(IDM_ABOUTBOX & 0xF

5、FF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);SetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small iconm_iNetPort = 3127;m_Server.Initialize(this);m_bServer = FALSE;GetDlgItem(IDC_BN_CLOSE)-EnableWindo

6、w(FALSE);return TRUE; / return TRUE unless you set the focus to a controlvoid CServerDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:OnSysCommand(nID, lParam);void CServerDlg:OnPaint() if (IsIconic()CPaintDC dc(this); / devi

7、ce context for paintingSendMessage(WM_ICONERASEBKGND, (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_hI

8、con);elseCDialog:OnPaint();HCURSOR CServerDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CServerDlg:OnBnStart() / 創(chuàng)建套接字m_Server.Create(m_iNetPort);/ 開(kāi)始監(jiān)聽(tīng)m_Server.Listen();/ 顯示CString strInfo;strInfo.LoadString(IDS_CREATE_SERVER);ShowInDisplay(strInfo);m_bServer = TRUE;GetDlgItem(IDC_BN_START)-En

9、ableWindow(FALSE);GetDlgItem(IDC_BN_CLOSE)-EnableWindow(TRUE);void CServerDlg:OnBnClose() CloseServer();GetDlgItem(IDC_BN_START)-EnableWindow(TRUE);GetDlgItem(IDC_BN_CLOSE)-EnableWindow(FALSE);void CServerDlg:OnAccept()/ 創(chuàng)建新客戶CMySocket *pNewClient = new CMySocket;pNewClient-Initialize(this);m_Server

10、.Accept(*pNewClient);/ 放入鏈表中m_lsClient.AddTail(pNewClient);void CServerDlg:OnClientClose(WPARAM wParam, LPARAM lParam)CMySocket *p_delClient = (CMySocket*)wParam;/ 關(guān)閉該用戶p_delClient-ShutDown();char bufferBUFFERSIZE;while(p_delClient-Receive(buffer, BUFFERSIZE)0);p_delClient-Close();/ 在用戶鏈表中刪除該用戶POSIT

11、ION psCur, psList = m_lsClient.GetHeadPosition();CMySocket *p_curClient;while(psList!=NULL)psCur = psList;p_curClient = (CMySocket *)m_lsClient.GetNext(psList);if(p_delClient=p_curClient)m_lsClient.RemoveAt(psCur);break;/ 發(fā)送信息告訴其他用戶該客戶退出CString strMsg;strMsg.LoadString(IDS_CLIENT_CLOSE);NETMESSAGE n

12、etMessage(PTC_CLIENT_QUIT, p_delClient-GetName(), _T(), p_delClient-GetName() + strMsg);SendToAllClient(netMessage);/ 界面上清理該用戶信息m_lbUser.DeleteString(m_lbUser.FindString(-1, p_delClient-GetName();ShowInDisplay(p_delClient-GetName() + strMsg);/ 刪除該用戶delete p_delClient;void CServerDlg:OnReceive(WPARAM

13、 wParam, LPARAM lParam)CMySocket *p_curClient = (CMySocket*)wParam;NETMESSAGE netMessage;p_curClient-Receive(char *)&netMessage, sizeof(netMessage);switch(netMessage.type)case PTC_NEW:/ 新客戶加入NewClientAdd(p_curClient, &netMessage);break;case PTC_SAY:/ 普通聊天ShowMessage(netMessage);break;/ 把加工過(guò)的信息發(fā)送給其他用

14、戶SendToAllClient(netMessage);void CServerDlg:CloseServer()if(!m_bServer) return;/ 向所有客戶發(fā)送服務(wù)端關(guān)閉的信息CString strInfo;strInfo.LoadString(IDS_CLOSESERVER);NETMESSAGE netMessage(PTC_SYSTEM, _T(), _T(), strInfo);SendToAllClient(netMessage);/ 服務(wù)端界面顯示關(guān)閉信息ShowInDisplay(strInfo);/ 關(guān)閉服務(wù)端m_Server.Close();/ 關(guān)閉客戶端c

15、har bufferBUFFERSIZE;CMySocket *m_pClient;POSITION psList = m_lsClient.GetHeadPosition();while(psList!=NULL)m_pClient = (CMySocket *)m_lsClient.GetNext(psList);m_pClient-ShutDown();while(m_pClient-Receive(buffer, BUFFERSIZE)0);m_pClient-Close();delete m_pClient;m_lsClient.RemoveAll();m_bServer = FAL

16、SE;/ 向所有戶發(fā)出信息void CServerDlg:SendToAllClient(const NETMESSAGE &netMessage)CMySocket *m_pClient;POSITION psList = m_lsClient.GetHeadPosition();while(psList!=NULL)m_pClient = (CMySocket *)m_lsClient.GetNext(psList);m_pClient-SendMsg(netMessage);void CServerDlg:ShowInDisplay(CString str)m_strDisplay +=

17、 str + rn;UpdateData(false);void CServerDlg:OnDestroy() CDialog:OnDestroy();/ TODO: Add your message handler code here/ 新用戶加入 void CServerDlg:NewClientAdd(CMySocket *p_client, NETMESSAGE *netMessage)/ 存入用戶名字p_client-SetName(netMessage-form);/ 在列表框中顯示該用戶m_lbUser.AddString(p_client-GetName();/ 發(fā)送用戶列表給

18、該客戶NETMESSAGE netMsg(PTC_USER_LIST, _T(), _T(), _T();int iListLen = m_lbUser.GetCount();CString strMsg;for(int index=m_lbUser.GetTopIndex(); indexSendMsg(netMsg);/ 加工信息strMsg.LoadString(IDS_NEW_CLIENT);strMsg = p_client-GetName() + strMsg;strcpy(netMessage-data, (LPCTSTR)strMsg);/ 顯示該用戶進(jìn)入ShowInDispl

19、ay(strMsg);/ 顯示普通消息void CServerDlg:ShowMessage(NETMESSAGE netMessage)CString strTo = netMessage.to;if(strTo=_T()strTo = 所有人;CString strForm = netMessage.form;CString strMsg = netMessage.data;CString strOut = strForm + 對(duì) + strTo + 說(shuō): + strMsg;ShowInDisplay(strOut);II客戶端:/ ClientDlg.cpp : implementati

20、on file/#include stdafx.h#include Client.h#include ClientDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();enum IDD = IDD_ABOUTBOX ;protected:virtual void DoDataExchange(

21、CDataExchange* pDX); / DDX/DDV supportprotected:DECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)void CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)END_MESSAGE_MAP()/ CClientDlg dialogCClientDlg:CClientDlg(CWnd* pParen

22、t /*=NULL*/): CDialog(CClientDlg:IDD, pParent)m_strDisplay = _T();m_strSend = _T();m_bWhispering = FALSE;m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CClientDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);DDX_Control(pDX, IDC_LIST_USER, m_lbUser);DDX_Text(pDX, IDC_EDIT_DIAPLA

23、Y, m_strDisplay);DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);DDX_Check(pDX, IDC_CHECK_WHISPERING, m_bWhispering);BEGIN_MESSAGE_MAP(CClientDlg, CDialog)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BN_CONNECT, OnBnConnect)ON_BN_CLICKED(IDC_BN_CLOSE, OnBnClose)ON_BN_CLICKED(IDC_BN_

24、SEND, OnBnSend)ON_MESSAGE(WM_RECEIVE, OnReceive)ON_MESSAGE(WM_SOCKETCLOSE, OnServerClose)ON_MESSAGE(WM_SEND, OnSend)ON_MESSAGE(WM_CONNCET, OnConnect)ON_LBN_DBLCLK(IDC_LIST_USER, OnDblclkListUser)END_MESSAGE_MAP()/ CClientDlg message handlersBOOL CClientDlg:OnInitDialog()CDialog:OnInitDialog();ASSERT

25、(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);SetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ 參數(shù)初始化m_Socket.Initialize(this);m_strNetIP = _T(127.0.0.1);m_iNetPort = 3127;m

26、_bConnect = FALSE;GetDlgItem(IDC_BN_CLOSE)-EnableWindow(FALSE);GetDlgItem(IDC_BN_SEND)-EnableWindow(FALSE);return TRUE; / return TRUE unless you set the focus to a controlvoid CClientDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseC

27、Dialog:OnSysCommand(nID, lParam);void CClientDlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);int cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x

28、 = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;dc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPaint();HCURSOR CClientDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CClientDlg:OnBnConnect() CString strConnectError;/ 獲取用戶名字CInputDlg inputDlg;int iResult = inputDlg.DoModal();

29、if(iResult=IDCANCEL)return;m_strName = inputDlg.m_strName;/ 創(chuàng)建套接字if(!m_Socket.Create()strConnectError.LoadString(IDS_FAIL_CREATE_SOCKET);ShowInDisplay(strConnectError);return;CString strInfo;strInfo.LoadString(IDS_CONNECTING);ShowInDisplay(strInfo);/ 連接服務(wù)器if(!m_Socket.Connect(m_strNetIP, m_iNetPort)

30、int n = m_Socket.GetLastError();strConnectError.LoadString(IDS_FAIL_CONNECT);ShowInDisplay(strConnectError);return;strInfo.LoadString(IDS_SUCCEED_CONNECT);ShowInDisplay(strInfo);m_bConnect = TRUE;/ 發(fā)送自已的名字NETMESSAGE netMessage(PTC_NEW, m_strName, _T(), _T();m_Socket.SendMsg(netMessage);GetDlgItem(ID

31、C_BN_CONNECT)-EnableWindow(FALSE);GetDlgItem(IDC_BN_CLOSE)-EnableWindow(TRUE);GetDlgItem(IDC_BN_SEND)-EnableWindow(TRUE);void CClientDlg:OnBnClose() if(m_bConnect) m_Socket.Close();m_bConnect = FALSE;GetDlgItem(IDC_BN_CONNECT)-EnableWindow(TRUE);GetDlgItem(IDC_BN_CLOSE)-EnableWindow(FALSE);GetDlgIte

32、m(IDC_BN_SEND)-EnableWindow(FALSE);m_lbUser.ResetContent();CString strMsg;strMsg.LoadString(IDS_BREAK);ShowInDisplay(strMsg);void CClientDlg:OnBnSend() OnSend();m_strSend = _T();UpdateData(FALSE);void CClientDlg:OnReceive()NETMESSAGE netMessage;m_Socket.Receive(char *)&netMessage, sizeof(netMessage)

33、;CString strForm = netMessage.form;/ 過(guò)濾自已發(fā)出的信息if(strForm=m_strName) return;CString strTo = netMessage.to;CString strMsg = netMessage.data;switch(netMessage.type)case PTC_USER_LIST:/ 用戶列表m_lbUser.AddString(strMsg);break;case PTC_NEW:/ 新用戶進(jìn)入m_lbUser.AddString(strForm);ShowInDisplay(strMsg);break;case

34、PTC_SAY:ShowMessage(netMessage);case PTC_WHISPERING:/ 密語(yǔ)if(strTo!=m_strName) return;ShowMessage(netMessage);break;case PTC_CLIENT_QUIT:/ 有用戶退出m_lbUser.DeleteString(m_lbUser.FindString(-1, strForm);ShowInDisplay(strMsg);break;void CClientDlg:OnServerClose()if(!m_bConnect) return;CString strMsg;strMsg.LoadString(IDS_SERVER_CLOSE);ShowInDisplay(strMsg);m_Socket.Close();m_bConnect = FALSE;GetDlgItem(IDC_BN_CONNECT)-EnableWindow(TRUE);GetDlgItem(IDC_BN_CLOSE)-EnableWindow(FALSE);GetDlgItem(IDC_BN_SEND)-EnableW

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論