基于MFC仿QQ聊天程序设计完整实例教程
- 格式:doc
- 大小:459.50 KB
- 文档页数:33
聊天程序设计报告---- 网络编程设计报告目录【1】设计原理———————————————————————— 1 【2】功能描述———————————————————————— 1 【3】详细设计———————————————————————— 2 【4】工作进度————————————————————————7 【5】设计总结————————————————————————8 【6】使用方法————————————————————————9 【7】附录——————————————————————————9一. 设计原理计算机网络技术发展至今已经大大超越了人们当初的预想,无论是人们日常的工作还是学习,我们都越来越多的依靠到互联网。
各种实时性的聊天娱乐软件也同时诞生,而且为我们的即时通讯带来了众多的方便,比如说大家所熟知的腾讯QQ、微软的MSN、中国移动的Fetion 等,都是做的比较成功的实时聊天工具。
随着网络的日益普及,各种聊天工具也层出不穷,但当我们学习了《网络编程》这门课程之后,我们便会觉得,其实要实现简单的网络通讯其实并不难。
接下来的课程设计就是针对一个简单的网络聊天程序,利用MFC 为开发工具,实现基本的通讯功能。
此程序主要分为两个模块:服务线程和聊天线程。
服务线程用于提供一个网络端口,等待客户线程发出请求,登录到作为服务端的程序,然后进行网络通讯和消息的收发;客户端可通过服务器端的IP 地址发送连接请求,然后建立连接,进行通讯。
整个程序的主体使用了CSocket类的方法,实现了网络通讯聊天。
二.功能描述(需求分析)1、一对一会话。
此程序既可以作为服务器,也可以作为客户端。
当发起聊天请求时是客户端,而当接受连接请求时时服务器。
通讯基于可靠的TCP 连接。
2、修改联系人通讯地址。
可以根据联系人IP 地址和端口的不同建立不同的连接。
可以实时修改联系人的地址。
3、多线程技术。
进程启动时会开启一个专门用于监听连接请求的服务进程,线程函数为UINT threadListen(LPVOID lpParam) ,用来被动接受聊天请求。
《网络程序设计》课程设计报告书题目:简单的聊天室程序专业:网络工程完成日期:一、题目:简单的聊天室程序要求:本题是一个简单的聊天室程序,采用客户/服务器模式,分为客户端程序和服务器端程序。
由于服务器只能支持一个客户,实际上是一个点对点通信的程序。
客户端程序和服务器程序通过网络交换聊天字符串内容,并在窗口的列表框中显示。
二、系统概要设计聊天室是分客户端和服务端两个应用程序的,两个应用程序要想实现交互必须编写相应的指令和识别指令的代码,我写的这是个指令依次是启动停止用户退出的命令,但用户想要进行以上动作中的任何一个时,在用户按下按键的时候,客户端都是向服务端发送相应的指令,再由服务端实际的执行。
三、系统详细设计对概要设计中提到的功能函数进行详细设计。
服务器端:// ChatRoomServerDlg.cpp : implementation file//#include "stdafx.h"#include "ChatRoomServer.h"#include "ChatRoomServerDlg.h"#include "ListenSocket.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CChatRoomServerDlg dialogCChatRoomServerDlg::CChatRoomServerDlg(CWnd* pParent /*=NULL*/) : CDialog(CChatRoomServerDlg::IDD, pParent){//{{AFX_DATA_INIT(CChatRoomServerDlg)// 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 CChatRoomServerDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CChatRoomServerDlg)DDX_Control(pDX, IDC_BUTTON_STOP, m_IDC_BUTTON_STOP);DDX_Control(pDX, IDC_BUTTON_START, m_IDC_BUTTON_START);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CChatRoomServerDlg, CDialog)//{{AFX_MSG_MAP(CChatRoomServerDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CChatRoomServerDlg message handlersBOOL CChatRoomServerDlg::OnInitDialog(){CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.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);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herem_IDC_BUTTON_STOP.EnableWindow(FALSE);return TRUE; // return TRUE unless you set the focus to a control}void CChatRoomServerDlg::OnSysCommand(UINT nID, LPARAM lParam)if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);}}// If you add a minimize button to your dialog, you will need the code below// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.void CChatRoomServerDlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint 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;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}}// The system calls this to obtain the cursor to display while the user drags// the minimized window.HCURSOR CChatRoomServerDlg::OnQueryDragIcon()return (HCURSOR) m_hIcon;}void CChatRoomServerDlg::OnButtonStart(){// TODO: Add your control notification handler code herem_IDC_BUTTON_START.EnableWindow(FALSE);//使启动按钮无效ListenSocket.Create(6767);//创建监听套接字端口为6767ListenSocket.Listen();//开始监听m_IDC_BUTTON_STOP.EnableWindow(TRUE);//将停止按钮激活}void CChatRoomServerDlg::OnButtonStop(){// TODO: Add your control notification handler code herem_IDC_BUTTON_STOP.EnableWindow(FALSE);//使停止按钮无效ListenSocket.Close();//关闭监听套接字m_IDC_BUTTON_START.EnableWindow(TRUE);//将启动按钮激活}// ChatRoomServer.h : main header file for the CHATROOMSERVER application//#if !defined(AFX_CHA TROOMSERVER_H__680EC642_E19B_4D55_88DF_2C9E9B1B30FD __INCLUDED_)#defineAFX_CHATROOMSERVER_H__680EC642_E19B_4D55_88DF_2C9E9B1B30FD__INCLUDE D_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#ifndef __AFXWIN_H__#error include 'stdafx.h' before including this file for PCH#endif#include "resource.h" // main symbols/////////////////////////////////////////////////////////////////////////////// CChatRoomServerApp:// See ChatRoomServer.cpp for the implementation of this class//class CChatRoomServerApp : public CWinApp{public:CChatRoomServerApp();// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CChatRoomServerApp)public:virtual BOOL InitInstance();//}}AFX_VIRTUAL// Implementation//{{AFX_MSG(CChatRoomServerApp)// NOTE - the ClassWizard will add and remove member functions here.// DO NOT EDIT what you see in these blocks of generated code !//}}AFX_MSGDECLARE_MESSAGE_MAP()};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCA TION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif// !defined(AFX_CHATROOMSERVER_H__680EC642_E19B_4D55_88DF_2C9E9B1B30FD__ INCLUDED_)// ChatRoomServerDlg.h : header file//#if !defined(AFX_CHA TROOMSERVERDLG_H__5BE648B6_8A9C_4E90_BF1D_20FE943A5 25F__INCLUDED_)#defineAFX_CHATROOMSERVERDLG_H__5BE648B6_8A9C_4E90_BF1D_20FE943A525F__INCL UDED_#include "ClientSocketList.h" // Added by ClassView#include "ListenSocket.h" // Added by ClassView#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////// CChatRoomServerDlg dialogclass CChatRoomServerDlg : public CDialog{// Constructionpublic:CListenSocket ListenSocket;CChatRoomServerDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CChatRoomServerDlg)enum { IDD = IDD_CHATROOMSERVER_DIALOG };CButton m_IDC_BUTTON_STOP;CButton m_IDC_BUTTON_START;//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CChatRoomServerDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected:HICON m_hIcon;// Generated message map functions//{{AFX_MSG(CChatRoomServerDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnButtonStart();afx_msg void OnButtonStop();//}}AFX_MSGDECLARE_MESSAGE_MAP()};//{{AFX_INSERT_LOCA TION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif// !defined(AFX_CHA TROOMSERVERDLG_H__5BE648B6_8A9C_4E90_BF1D_20FE943A52 5F__INCLUDED_)#if !defined(AFX_CLIENTSOCKET_H__5B707F31_3AD5_4F47_B58E_ECFC99EB60F0__IN CLUDED_)#defineAFX_CLIENTSOCKET_H__5B707F31_3AD5_4F47_B58E_ECFC99EB60F0__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// ClientSocket.h : header file///////////////////////////////////////////////////////////////////////////////// CClientSocket command targetclass CClientSocketList;class CClientSocket : public CSocket{// Attributespublic:// Operationspublic:CClientSocket(CClientSocketList *);virtual ~CClientSocket();// Overridespublic:CClientSocketList *List;CClientSocket * Front;CClientSocket * Next;// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CClientSocket)public:virtual void OnReceive(int nErrorCode);virtual void OnClose(int nErrorCode);//}}AFX_VIRTUAL// Generated message map functions//{{AFX_MSG(CClientSocket)// NOTE - the ClassWizard will add and remove member functions here.//}}AFX_MSG// Implementationprotected:};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCA TION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif// !defined(AFX_CLIENTSOCKET_H__5B707F31_3AD5_4F47_B58E_ECFC99EB60F0__INC LUDED_)// ClientSocketList.h: interface for the CClientSocketList class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_CLIENTSOCKETLIST_H__E746355D_FA10_4D12_B544_2FF152C16414__ INCLUDED_)#defineAFX_CLIENTSOCKETLIST_H__E746355D_FA10_4D12_B544_2FF152C16414__INCLUDE D_#include "ClientSocket.h"#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class CClientSocketList{public:BOOL Sends(CClientSocket *);BOOL Add(CClientSocket *);CClientSocket * Head;CClientSocketList();virtual ~CClientSocketList();};#endif// !defined(AFX_CLIENTSOCKETLIST_H__E746355D_FA10_4D12_B544_2FF152C16414__I NCLUDED_)#if !defined(AFX_LISTENSOCKET_H__5D655304_370E_4680_A556_E417552D24EC__INC LUDED_)#defineAFX_LISTENSOCKET_H__5D655304_370E_4680_A556_E417552D24EC__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// ListenSocket.h : header file//#include "ClientSocketList.h"/////////////////////////////////////////////////////////////////////////////// CListenSocket command targetclass CListenSocket : public CSocket{// Attributespublic:// Operationspublic:CClientSocketList CCSL;//客户socket列表CListenSocket();virtual ~CListenSocket();// Overridespublic:// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CListenSocket)public:virtual void OnAccept(int nErrorCode); //重载OnAccept函数//}}AFX_VIRTUAL// Generated message map functions//{{AFX_MSG(CListenSocket)// NOTE - the ClassWizard will add and remove member functions here.//}}AFX_MSG// Implementationprotected:};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCA TION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif// !defined(AFX_LISTENSOCKET_H__5D655304_370E_4680_A556_E417552D24EC__INCL UDED_)// ChatRoomServer.cpp : Defines the class behaviors for the application.//#include "stdafx.h"#include "ChatRoomServer.h"#include "ChatRoomServerDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CChatRoomServerAppBEGIN_MESSAGE_MAP(CChatRoomServerApp, CWinApp)//{{AFX_MSG_MAP(CChatRoomServerApp)// NOTE - the ClassWizard will add and remove mapping macros here.// DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSGON_COMMAND(ID_HELP, CWinApp::OnHelp)END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CChatRoomServerApp constructionCChatRoomServerApp::CChatRoomServerApp(){// TODO: add construction code here,// Place all significant initialization in InitInstance}/////////////////////////////////////////////////////////////////////////////// The one and only CChatRoomServerApp objectCChatRoomServerApp theApp;/////////////////////////////////////////////////////////////////////////////// CChatRoomServerApp initializationBOOL CChatRoomServerApp::InitInstance(){if (!AfxSocketInit()){AfxMessageBox(IDP_SOCKETS_INIT_FAILED);return FALSE;}AfxEnableControlContainer();// Standard initialization// If you are not using these features and wish to reduce the size// of your final executable, you should remove from the following// the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls(); // Call this when using MFC in a shared DLL #elseEnable3dControlsStatic(); // Call this when linking to MFC statically#endifCChatRoomServerDlg dlg;m_pMainWnd = &dlg;int nResponse = dlg.DoModal();if (nResponse == IDOK){// TODO: Place code here to handle when the dialog is// dismissed with OK}else if (nResponse == IDCANCEL){// TODO: Place code here to handle when the dialog is// dismissed with Cancel}// Since the dialog has been closed, return FALSE so that we exit the// application, rather than start the application's message pump.return FALSE;}II 客户端:客户端:// ChatRoomClient.cpp : Defines the class behaviors for the application.//#include "stdafx.h"#include "ChatRoomClient.h"#include "ChatRoomClientDlg.h"#include "ConnectedDlg.h"#include "ClientSocket.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifCClientSocket curSocket;/////////////////////////////////////////////////////////////////////////////// CChatRoomClientAppBEGIN_MESSAGE_MAP(CChatRoomClientApp, CWinApp)//{{AFX_MSG_MAP(CChatRoomClientApp)// NOTE - the ClassWizard will add and remove mapping macros here.// DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSGON_COMMAND(ID_HELP, CWinApp::OnHelp)END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CChatRoomClientApp constructionCChatRoomClientApp::CChatRoomClientApp(){// TODO: add construction code here,// Place all significant initialization in InitInstance}/////////////////////////////////////////////////////////////////////////////// The one and only CChatRoomClientApp objectCChatRoomClientApp theApp;/////////////////////////////////////////////////////////////////////////////// CChatRoomClientApp initializationBOOL CChatRoomClientApp::InitInstance(){if (!AfxSocketInit()){AfxMessageBox(IDP_SOCKETS_INIT_FAILED);return FALSE;}AfxEnableControlContainer();// Standard initialization// If you are not using these features and wish to reduce the size// of your final executable, you should remove from the following// the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls(); // Call this when using MFC in a shared DLL #elseEnable3dControlsStatic(); // Call this when linking to MFC statically#endifCConnectedDlg cdlg(&curSocket);if (cdlg.DoModal()==IDCANCEL) return FALSE;CChatRoomClientDlg dlg(&curSocket);m_pMainWnd = &dlg;curSocket.SetDlg(&dlg);int nResponse = dlg.DoModal();if (nResponse == IDOK){// TODO: Place code here to handle when the dialog is// dismissed with OK}else if (nResponse == IDCANCEL){// TODO: Place code here to handle when the dialog is// dismissed with Cancel}// Since the dialog has been closed, return FALSE so that we exit the// application, rather than start the application's message pump.return FALSE;}// ChatRoomClientDlg.cpp : implementation file//#include "stdafx.h"#include "ChatRoomClient.h"#include "ChatRoomClientDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog{public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}void CAboutDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CChatRoomClientDlg dialogCChatRoomClientDlg::CChatRoomClientDlg(CClientSocket *tmp,CWnd* pParent /*=NULL*/) : CDialog(CChatRoomClientDlg::IDD, pParent){//{{AFX_DATA_INIT(CChatRoomClientDlg)m_IDC_EDIT_MESSAGE = _T("");//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);myServerSocket=tmp;}void CChatRoomClientDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CChatRoomClientDlg)DDX_Control(pDX, IDC_STATIC_NIKENAME, m_STATIC_NIKENAME_CONTROL);DDX_Control(pDX, IDC_LIST_CHA TBOX, m_IDC_LIST_CHATBOX_CONTROL);DDX_Text(pDX, IDC_EDIT_MESSAGE, m_IDC_EDIT_MESSAGE);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CChatRoomClientDlg, CDialog)//{{AFX_MSG_MAP(CChatRoomClientDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CChatRoomClientDlg message handlersBOOL CChatRoomClientDlg::OnInitDialog(){CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.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);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small iconm_STATIC_NIKENAME_CONTROL.SetWindowText(myServerSocket->NikeName);// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control}void CChatRoomClientDlg::OnSysCommand(UINT nID, LPARAM lParam){if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);}}// If you add a minimize button to your dialog, you will need the code below// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.void CChatRoomClientDlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint 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;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}}// The system calls this to obtain the cursor to display while the user drags// the minimized window.HCURSOR CChatRoomClientDlg::OnQueryDragIcon(){return (HCURSOR) m_hIcon;}BOOL CChatRoomClientDlg::GetMessage(){char buff[1000];int count;count=myServerSocket->Receive(buff,1000);buff[count]=0;m_IDC_LIST_CHA TBOX_CONTROL.AddString(buff);return true;void CChatRoomClientDlg::OnButtonSend(){// TODO: Add your control notification handler code hereint n;char message[1000];UpdateData(TRUE);m_IDC_EDIT_MESSAGE=myServerSocket->NikeName+": "+m_IDC_EDIT_MESSAGE;n=m_IDC_EDIT_MESSAGE.GetLength();//message=new char(n+1);sprintf(message,"%s",m_IDC_EDIT_MESSAGE.GetBuffer(n));message[n]=0;if (myServerSocket->Send(message,n+1)){m_IDC_EDIT_MESSAGE="";UpdateData(FALSE);}else{AfxMessageBox("网络传输错误!!!");}}// ClientSocket.cpp : implementation file//#include "stdafx.h"#include "ChatRoomClient.h"#include "ClientSocket.h"#include "ChatRoomClientDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CClientSocketCClientSocket::CClientSocket()NikeName="";myDlg=0;}CClientSocket::~CClientSocket(){}BOOL CClientSocket::SetDlg(CChatRoomClientDlg *tmp){myDlg=tmp;return true;}// Do not edit the following lines, which are needed by ClassWizard.#if 0BEGIN_MESSAGE_MAP(CClientSocket, CSocket)//{{AFX_MSG_MAP(CClientSocket)//}}AFX_MSG_MAPEND_MESSAGE_MAP()#endif // 0/////////////////////////////////////////////////////////////////////////////// CClientSocket member functionsvoid CClientSocket::OnReceive(int nErrorCode){// TODO: Add your specialized code here and/or call the base class myDlg->GetMessage();CSocket::OnReceive(nErrorCode);}// ConnectedDlg1.cpp : implementation file//#include "stdafx.h"#include "ChatRoomClient.h"#include "ConnectedDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CConnectedDlg dialogCConnectedDlg::CConnectedDlg(CClientSocket * tmp,CWnd* pParent /*=NULL*/) : CDialog(CConnectedDlg::IDD, pParent){//{{AFX_DATA_INIT(CConnectedDlg)m_IDC_EDIT_ADDRESS = _T("");m_IDC_EDIT_NIKENAME = _T("");//}}AFX_DATA_INITmyServerSocket=tmp;}void CConnectedDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CConnectedDlg)DDX_Text(pDX, IDC_EDIT_ADDRESS, m_IDC_EDIT_ADDRESS);DDX_Text(pDX, IDC_EDIT_NIKENAME, m_IDC_EDIT_NIKENAME);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CConnectedDlg, CDialog)//{{AFX_MSG_MAP(CConnectedDlg)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CConnectedDlg message handlersvoid CConnectedDlg::OnOK(){// TODO: Add extra validation hereUpdateData(TRUE);char *nikename,*address;int n;if (!myServerSocket->Create()){myServerSocket->Close();AfxMessageBox("网络创建错误!!");return;}n=m_IDC_EDIT_ADDRESS.GetLength();address=new char(n+1);sprintf(address,"%s",m_IDC_EDIT_ADDRESS.GetBuffer(n));address[n]=0;n=m_IDC_EDIT_NIKENAME.GetLength();nikename=new char(n+1);sprintf(nikename,"%s",m_IDC_EDIT_NIKENAME.GetBuffer(n));nikename[n]=0;if (!myServerSocket->Connect(address,6767)){myServerSocket->Close();AfxMessageBox("网络连接错误!请重新检查服务器地址的填写是否正确?");return;}myServerSocket->NikeName=nikename;CDialog::OnOK();}// stdafx.cpp : source file that includes just the standard includes// ChatRoomClient.pch will be the pre-compiled header// stdafx.obj will contain the pre-compiled type information#include "stdafx.h"// ChatRoomClient.h : main header file for the CHATROOMCLIENT application//#if !defined(AFX_CHATROOMCLIENT_H__4503F0DD_CA95_4F24_B6EE_3183B64B095B_ _INCLUDED_)#defineAFX_CHATROOMCLIENT_H__4503F0DD_CA95_4F24_B6EE_3183B64B095B__INCLUDE D_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#ifndef __AFXWIN_H__#error include 'stdafx.h' before including this file for PCH#endif#include "resource.h" // main symbols/////////////////////////////////////////////////////////////////////////////// CChatRoomClientApp:// See ChatRoomClient.cpp for the implementation of this class//class CChatRoomClientApp : public CWinApp{public:CChatRoomClientApp();// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CChatRoomClientApp)public:virtual BOOL InitInstance();//}}AFX_VIRTUAL// Implementation//{{AFX_MSG(CChatRoomClientApp)// NOTE - the ClassWizard will add and remove member functions here.// DO NOT EDIT what you see in these blocks of generated code !//}}AFX_MSGDECLARE_MESSAGE_MAP()};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCA TION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif// !defined(AFX_CHATROOMCLIENT_H__4503F0DD_CA95_4F24_B6EE_3183B64B095B__I NCLUDED_)// ChatRoomClientDlg.h : header file//#if !defined(AFX_CHATROOMCLIENTDLG_H__4DF07865_B696_4641_82F8_4B32BF39595 9__INCLUDED_)#defineAFX_CHATROOMCLIENTDLG_H__4DF07865_B696_4641_82F8_4B32BF395959__INCLU DED_#include "ClientSocket.h" // Added by ClassView#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000//class ClientSocket;/////////////////////////////////////////////////////////////////////////////// CChatRoomClientDlg dialogclass CChatRoomClientDlg : public CDialog{// Constructionpublic:CClientSocket * myServerSocket;BOOL GetMessage();CChatRoomClientDlg(CClientSocket *,CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CChatRoomClientDlg)enum { IDD = IDD_CHATROOMCLIENT_DIALOG };CStatic m_STATIC_NIKENAME_CONTROL;CListBox m_IDC_LIST_CHA TBOX_CONTROL;CString m_IDC_EDIT_MESSAGE;//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CChatRoomClientDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected:HICON m_hIcon;// Generated message map functions//{{AFX_MSG(CChatRoomClientDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnButtonSend();//}}AFX_MSGDECLARE_MESSAGE_MAP()};//{{AFX_INSERT_LOCA TION}}。
c 仿qq课程设计一、课程目标知识目标:1. 学生能理解并掌握C语言中结构体和函数的基本概念及其在编程中的应用。
2. 学生能够运用C语言实现基本的聊天功能,如发送消息、接收消息等。
3. 学生了解并掌握简单的文件读写操作,实现对聊天记录的保存和加载。
技能目标:1. 学生通过本课程的学习,培养编程思维和问题解决能力,能够运用C语言设计并实现类似QQ的简易聊天程序。
2. 学生能够运用所学知识,进行程序调试和优化,提高程序性能和稳定性。
3. 学生能够通过团队协作,共同完成课程项目,提高沟通与协作能力。
情感态度价值观目标:1. 学生在学习过程中,培养对编程的兴趣和热情,提高信息素养。
2. 学生通过课程实践,认识到编程对于解决实际问题的价值,增强自信心。
3. 学生在团队合作中,学会尊重他人意见,培养集体荣誉感和责任感。
课程性质:本课程为C语言编程实践课,旨在通过实现类似QQ的聊天功能,让学生将所学理论知识运用到实际项目中。
学生特点:学生为初中生,具备一定的C语言基础,对编程感兴趣,喜欢动手实践。
教学要求:结合学生特点,课程设计应注重实践性和趣味性,引导学生主动探索,提高编程能力和团队协作能力。
通过分解课程目标,使学生在完成具体学习成果的过程中,达到课程目标。
后续教学设计和评估将以此为基础,确保课程目标的实现。
二、教学内容1. 结构体和函数复习:回顾C语言中结构体的定义和使用,以及函数的声明、定义和调用,为后续实现聊天功能打下基础。
(对应教材章节:结构体与函数)2. 聊天功能实现:- 消息发送与接收:介绍标准输入输出函数,实现用户之间的消息发送与接收。
- 用户登录与注册:结合结构体,设计用户信息数据结构,实现用户登录与注册功能。
(对应教材章节:文件操作、控制语句)3. 聊天记录保存与加载:- 文件读写:介绍文件操作函数,实现对聊天记录的保存和加载功能。
- 数据存储格式:讲解如何选择合适的数据存储格式,提高数据的可读性和可维护性。
实战2012高技术的Hacker黄今天无私的教大家利用C++的MFC做局域网的QQ软件QQ很难吗?C++很难吗?MFC很难吗?做个软件很难吗?答案是不难,因为今天高技术的Hacker黄今天教大家利用C++的MFC做局域网的QQ软件。
废话少说,现在直接带领大家实战做个局域网的QQ软件用C++。
事件2012年11月14号夜晚,湖北大学实验室,实验结果做出一个如下截图的QQ 软件事件发起人hacker黄下面为了大家更好的理解实战类容,首先我将按逻辑的将本次试验叙述下来,不怕不懂,我将把每一步的截图和代码打出来,大家只需要看着截图,结合代码就行了哦哈哈施展正式开始:第一做局域网QQ界面打开vc6.0新建一个工程文件第二选择对话框,点击界面接好了第三删去对话框中的初始控件,第三添加一个组框控件截图如下第四设置组框组建的属性截图如下,并将其CAPTION属性设置为接收数据截图如下第五添加一个编辑框控件并设置其属性截图如下,并将其ID号吗改为IDC_EDIT_RECV截图如下用来显示用户接收到的数据第六再次添加一个组框并将其属性设置为发送数据第七在发送数据组框添加一个IP控件第八在选择一个一个编辑框控件,并设置其ID号,用来放松疏忽需要发送的数据截图如下第九添加一个发送的按钮,并将其ID号吗改为IDC_BTN_SEND第十展示最终的C++MFCQQ界面截图如下第十一加载套接字库截图如下,并添加代码如下补加的的代码如下if(!AfxSocketInit()){AfxMessageBox("加载套接字库失败");return FALSE;}第十二增加一头文件,截图及代码如下代码如下#include<Afxsock.h>第十三增加一个成员变量的函数及代码第十四增加一个成员变量并将其权限设为私有截图如下第十五在初始化套接字函数中输入初始化相关代码m_socket=socket(AF_INET,SOCK_DGRAM,0); if(INVALID_SOCKET==m_socket){MessageBox("套接字失败");return FALSE;SOCKADDR_IN addrSock;addrSock.sin_family=AF_INET;addrSock.sin_port=htons(6000);addrSock.sin_addr.S_un .S_addr =htonl(INADDR_ANY);int retval;retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));if(SOCKET_ERROR==retval){closesocket(m_socket);MessageBox("绑定失败");return FALSE;}return TRUE;第十六调用初始化套接字函数在ONITDIALOGInitSocket();到此套接字的初始化工作全部完成了第十七开始编写接收端的程序创建线程函数及代码如下首先定义一个结构体struct RECVPARAM{SOCKET sock;HWND hwnd;};第十八在ONINITDIAG中创建一个指针RECVPARAM *pRecvParam=new RECVPARAM;pRecvParam->sock=m_socket;pRecvParam->hwnd=m_hWnd;HANDLEhThread=CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL);CloseHandle(hThread);第十九开始写线程函数,现增加一个成员函数以相关代码,如下第二十修还代码中的2个地方分别是如下第二十一再次运行又有一个错误在改正,这次是变函数为静态变量第二十二再创建的函数中输入相关代码,及服务端主要的代码SOCKET sock=((RECVPARAM*)IpParameter)->sock;HWND hwnd=((RECVPARAM*)IpParameter)->hwnd;SOCKADDR_IN addrFrom;int len =sizeof(SOCKADDR);char recvBuf[200];char tempBuf[300];int retval;while(TRUE){retval =recvfrom(sock,recvBuf,200,0,(SOCKADDR*)&addrFrom,&len);if(SOCKET_ERROR==retval)break;sprintf(tempBuf,"%s说:%s",inet_ntoa(addrFrom.sin_addr),recvBuf);::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM)tempBuf);}第二十三定义消息得值#define WM_RECVDATA WM_USER+1第二十四做一个消息相应的函数afx_msg void OnRecvData(WPARAM wParam,LPARAM lParam);第二十五写一个消息映射的函数截图代码如下ON_MESSAGE(WM_RECVDATA,OnRecvData)第二十六消息响应函数的实现代码代码如下void CChatDlg :: OnRecvData(WPARAM wParam,LPARAM lParam) {CString str=(char*)lParam;CString strTemp;GetDlgItemText(IDC_EDIT_RECV,strTemp);str +="\r\n";str +=strTemp;SetDlgItemText(IDC_EDIT_RECV,str);}第二十七哈哈总结下至此服务端全部编写结束,程序调试正确第二十八开始编写发送端进入对话框页面单机发送按钮发送按钮函数第二十九编写发送端的函数代码// TODO: Add your control notification handler code hereDWORD dwIP;((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP);SOCKADDR_IN addrTo;addrTo.sin_family=AF_INET;addrTo.sin_port=htons(6000);addrTo.sin_addr.S_un .S_addr =htonl(dwIP);CString strSend;GetDlgItemText(IDC_EDIT_SEND,strSend);sendto(m_socket,strSend,strSend.GetLength()+1,0,(SOCKADDR*)&addrTo,sizeof(S OCKADDR));SetDlgItemText(IDC_EDIT_SEND,"");第三十至此发送端的让软件也编写完成第三十一我们运行我们编写好的软件如下* *。
本文部分内容来自网络整理,本司不为其真实性负责,如有异议或侵权请及时联系,本司将立即删除!== 本文为word格式,下载后可方便编辑和修改! ==winform仿qq聊天气泡篇一:winform实现QQ截图代码winformêμ??QQ??í?′ú??.txtD??àê?ò?×ùóDá?????êòμ?·?×ó£?ò???×?×?í′?à£?ò???×?×??ìà??£è?2??üD|μ?ì??ì£?·??ò?á33D???±úμ?í′?à?£using System;using System.Drawing; using System.Collections; usingponentModel; using System.Windows.Forms; usingSystem.Data; using System.Runtime.InteropServices;namespaceCaptureScreen {/// <summary>/// CaptureScreenFormμ??aòa?μ?÷?£/// </summary>public class CaptureScreenForm :System.Windows.Forms.Form{ privateSystem.Windows.Forms.PictureBox picbox; privateSystem.Windows.Forms.Button exitBtn; privateSystem.Windows.Forms.Button aboutBtn; privateSystem.Windows.Forms.Button ClearBtn; privateSystem.Windows.Forms.Button saveBtn; privateSystem.Windows.Forms.Button fullScreenBtn; privateSystem.Windows.Forms.Button mouseCaptureBtn; public static Imageimage = null ; private System.Windows.Forms.Panel panel1; ///<summary> /// ±?Dèμ?éè???÷±?á??£ /// </summary> privateponentModel.Container components = null; publicCaptureScreenForm() { // // Windows ′°ì?éè???÷? 3??ù±?Dèμ?// InitializeComponent();// // TODO: ?úInitializeComponent μ÷ó?oóìí?óè?o?11?ìoˉêy′ú?? // } ///<summary> /// ??àí?ùóD?y?úê1ó?μ?×ê?′?£ /// </summary>protected override void Dispose( bool disposing ){ if( disposing ) {if (components != null) { components.Dispose();} }base.Dispose( disposing ); } #region Windows′°ì?éè???÷éú3éμ?′ú?? /// <summary> /// éè???÷? 3??ùDèμ?·?·¨ -2?òaê1ó?′ú??±à?-?÷DT?? /// ′?·?·¨μ??úèY?£ /// </summary>private void InitializeComponent() { this.picbox = newSystem.Windows.Forms.PictureBox(); this.fullScreenBtn = newSystem.Windows.Forms.Button(); this.mouseCaptureBtn = newSystem.Windows.Forms.Button(); this.ClearBtn = newSystem.Windows.Forms.Button(); this.saveBtn = newSystem.Windows.Forms.Button(); this.aboutBtn = newSystem.Windows.Forms.Button(); this.exitBtn = newSystem.Windows.Forms.Button(); this.panel1 = newSystem.Windows.Forms.Panel(); this.panel1.SuspendLayout();this.SuspendLayout(); //// picbox //this.picbox.Location = new System.Drawing.Point(0, 40); = "picbox"; this.picbox.Size = newSystem.Drawing.Size(680, 312); this.picbox.SizeMode =System.Windows.Forms.PictureBoxSizeMode.AutoSize;this.picbox.TabIndex = 0; this.picbox.TabStop = false; //// fullScreenBtn //this.fullScreenBtn.Location = newSystem.Drawing.Point(0, 0); = "fullScreenBtn"; this.fullScreenBtn.Size = newSystem.Drawing.Size(96, 32); this.fullScreenBtn.TabIndex = 1;this.fullScreenBtn.Text = "è??á??è?"; this.fullScreenBtn.Click += new System.EventHandler(this.fullScreenBtn_Click); //// mouseCaptureBtn //this.mouseCaptureBtn.Location = newSystem.Drawing.Point(120, 0); = "mouseCaptureBtn"; this.mouseCaptureBtn.Size = newSystem.Drawing.Size(96, 32); this.mouseCaptureBtn.TabIndex = 2; this.mouseCaptureBtn.Text = "êó±ê??è?";this.mouseCaptureBtn.Click += newSystem.EventHandler(this.mouseCaptureBtn_Click); ////ClearBtn //this.ClearBtn.Location = newSystem.Drawing.Point(240, 0); = "ClearBtn";this.ClearBtn.Size = new System.Drawing.Size(88, 32);this.ClearBtn.TabIndex = 3; this.ClearBtn.Text = "??3y??è?í???"; this.ClearBtn.Click += new System.EventHandler(this.ClearBtn_Click); //// saveBtn //this.saveBtn.Location = newSystem.Drawing.Point(352, 0); = "saveBtn";this.saveBtn.Size = new System.Drawing.Size(96, 32);this.saveBtn.TabIndex = 4; this.saveBtn.Text = "±£′???è?í???"; this.saveBtn.Click += new System.EventHandler(this.saveBtn_Click); //// aboutBtn //this.aboutBtn.Location = newSystem.Drawing.Point(472, 0); = "aboutBtn";this.aboutBtn.Size = new System.Drawing.Size(96, 32);this.aboutBtn.TabIndex = 5; this.aboutBtn.Text = "1? óú";this.aboutBtn.Click += new System.EventHandler(this.aboutBtn_Click); //// exitBtn //this.exitBtn.Location = newSystem.Drawing.Point(592, 0); = "exitBtn";this.exitBtn.Size = new System.Drawing.Size(88, 32);this.exitBtn.TabIndex = 6; this.exitBtn.Text = "í? 3?";this.exitBtn.Click += new System.EventHandler(this.exitBtn_Click); //// panel1 //this.panel1.Controls.Add(this.fullScreenBtn);this.panel1.Controls.Add(this.mouseCaptureBtn);this.panel1.Controls.Add(this.ClearBtn);this.panel1.Controls.Add(this.saveBtn);this.panel1.Controls.Add(this.aboutBtn);this.panel1.Controls.Add(this.exitBtn); this.panel1.Location =new System.Drawing.Point(0, 0); = "panel1";this.panel1.Size = new System.Drawing.Size(680, 32);this.panel1.TabIndex = 7; //// CaptureScreenForm //this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.AutoScroll = true; this.ClientSize = new。
MFC聊天软件课程设计网络聊天程序设计面向对象程序设计课程设计设计说明书网络聊天程序设计起止日期:2012年5月27日至2012年6月1日学生姓名何刘根班级计本103班学号10408100301 成绩指导教师(签字)计算机与通信学院2012 年 5 月30 日网络聊天程序设计一、课题的介绍和课题的任务课题介绍:近年来随着PC的广泛应用和网络的不断发展,目前网络技术已经成为了计算机领域内的最热门的发展方向。
聊天室。
在网络早期就开始盛行,不论是BBS,IRC 都有类似网上聊天室的机制。
聊天室适用于网上会议或闲聊的一些场合。
聊天室为网友提供了较好的交友环境,网友之间相互以文字交谈,在形式上有点类似笔友的性质,却大大节省了书信往返的时间,这也是网上交友之所以如此热门的原因。
课题任务:1、综合运用本课程及计算机网络的相关知识设计并实现一个网络应用程序,以Visual C++6.0 作为开发平台,通过实践复习巩固课堂所学的理论知识,提高对所学知识的综合应用能力,进一步加强winsock 编程技术。
2、采用客户/ 服务器模式,分为客户端程序和服务器程序。
服务器采用WINSOCK I/O莫型中的任一种,支持多个客户同时在线聊天。
客户端程序和服务器程序通过网络交换聊天字符串内容,服务器窗口的列表框中显示当前在线用户,支持服务器与客户端之间的私聊二、设计的要求1、用WINSOC实现简单的网络聊天;2、在MFC开发平台上编写一个聊天系统;3、得server 和client 之间可以相互通信;4、多人能够发送信息至服务器,首先显示进入聊天室的成员名,然后显示对方名字和聊天信息,可以并发执行。
5、服务器与单个客户端之间可以进行私聊三、系统的分析和系统中类的设计在此主要介绍服务器端的构建(即本人所负责的部分),不是本人负责的部分不过多介绍建立连接过程流程图如下:1Server 端Client 端创建CClientSocket对象,在某个端口提供监听服务等待来自Client端的服务请求创建CChatSocket 对象,向Server 的监听端口发出请求接受Client 端的请建立连接求,用返回的CClientSocket 建立连接向用向用CChatSocket 数据通路CClientSocket 连连接建立的流中读接建立的流中读写数据写数据关闭关闭CClientSocket,拆除链路CChatSocket,结结束与当前Client的束与当前Server 端通信,等待其他请求的通信关闭CClientSocket 对象,结束监听服务程序个模块代码如下:1. 客户端(1)通过AppWizard 生成基于对话框的应用程序ChatClient ,在向导的第二步选择WindowsSockets的支持,其余步骤均用默认值。
仿QQ聊天系统课程设计D必须准确地,完整地体现出用户的要求。
1.1软件功能需求分析通过对用户需求的市场调查的分析,确定反应用户需求的系统逻辑模型,把整个设计划分成3个单元,分别是程序启动、用户界面、后台服务。
使用Android 和java编程语言及网络数据库相结合,编写一个基于android端的移动聊天系统软件。
这个系统提供多个用户及时并发通信的平台。
未注册用户可以注册成系统成员,已注册用户可以通过登录进入聊天系统。
在系统中用户可以选择与某一个用户聊天,用户可以查找好友或附近的人,可以添加好友或把好友加黑名单等。
➢用户注册:用户在注册界面填入相关正确信息后,后台对其进行处理。
➢用户登录:用户输入自己的QQ帐号及密码后,点击登录,后台对其进行验证,以及好友的查找,登录成功后返回该用户的会话界面。
➢聊天:用户选定聊天对象后,对其发送信息,信息将由后台转发给选定的聊天对象,进行,提示,显示。
➢后台运行用户可以退出主界面,将聊天软件在后台运行,当有消息传入时,消息会用广播的形势显示。
➢后台管理:对用户的管理。
1.2 安全需求分析由于Android系统自身具备的开放源码的特征,它的安全性能成为信息安全领域研究的一个重要课题。
Android的安全性能主要体现在两个方面:Android 的系统安全和数据安全。
Android系统安全是指智能终端本身的安全,是对操作系统的保护,防止未授权的访问及对授权用户服务的拒绝或对未授权用户服务的允许,包括行为检测、记录等措施。
Android的数据安全指确保存储数据完整性、合法性二个方面,要求做到系统正确地传输数据,授权程序顺利地读取数据。
Android安全中的一个重要的设计点是在默认的情况下应用没有权限执行对其他应用程序、操作系统或用户的有害操作,这些操作包括读取用户的隐私数据,访问网络,保持设备活动等等,应用程序的进程是一个安全的黑盒子,在没有给它权限的时候它不能干扰其他应用程序。
仿QQ即时通讯应用软件开发摘要:本文按照软件工程的思想和开发步骤,采用C/S模式,利用SQL Server数据库,使用Visual C++开发工具设计并实现了一个仿QQ的即时通讯应用软件。
该软件实现了消息的即时传输、文本传输、语音聊天等功能,具体包括网络通信、协议包封装与解析、文件传输以及基于directsound框架的音频处理。
系统为保证语音传输的质量和效率,采用一定的声音编码格式对声音数据打包并传输到接收方。
该系统功能全面,界面美观,操作方便,具有一定的实用价值。
关键词:socket, 文字聊天,文件传输,实时语音传输,directsoundAbstract: In this paper, according to the software engineering and development steps, using C / S mode, the SQL Server database and Visual C++ language the system was designed and implement.The software has implemented the real-time transmission, transmission of text messages, voice chat and other functions, such as network communications, protocol packet encapsulation and analysis, file transfer and frame-based audio processing directsound.To ensure the quality and efficiency of the voice transmission the system adopt a certain voice encoding format for audio coding packaged and transmitted to the receiver side. The system is fully functional, beautiful interface, easy to operate, and has a certain practical value.Keywords: socket, text chat,file transfer, real-time voice transmission, directsound一、概述互联网的迅速发展不仅改变了人们的意识形态,更影响着人们的生活方式,网络元素在不知不觉中渗透到生活的每一个角落。
C 语言究竟能干什么序言鉴于现在已经大三了,很多同学很迷茫,自己学的东西到底能做什么,将来自己到底能干什么?我不想看着同学迷茫的面孔,特别是几个好兄弟,有几个想学习编程,但又苦苦找不到门路的兄弟,所以想写点东西,希望对大家又点略微的帮助,以尽兄弟我的微薄之力。
很多同学学了C语言之后,可能难免会有所感叹:这就是C语言!总是感觉C语言竟然能写出Windows、Linux?为了解除同学们的疑惑,也愿为同学们指点编程之道吧。
我写的这些东西采用C语言,计划通过编程实例来讲解C编程的一些知识,让大家对C能又更深一层的理解和认识。
当然,大家不要指望看了这些之后会写出一个操作系统来,但是我想,如果你认真看了的话,写一个类似与QQ的聊天程序应该不难。
本来书是假期里照顾妈妈时写的,原想是1、不让自己的水平停滞不前,温故知新(两个月的假期是很长的)2. 帮助一些同学,解决编程上的困惑3.希望妈妈快点康复4.让母亲和家里人知道自己一直都很努力,我是好样的但现在母亲已经能够不在了,所以第三点,就改为原母亲在天之灵得到慰藉,早日放下烦恼,不用惦记我们。
C语言的基本语法我是不打算再提了,很多C语言编程的书,就是将一些基本的数据类型、数据结构、语法,然后就是一些数值计算的实例,大多数都是雷同的,难免有抄袭之嫌,而且页没有多少实用价值。
本书以实用实例作为编程指导,指引大家编写真正实用的程序。
了解到大家对黑客程序、病毒、窗口类程序比较感兴趣,因此我就拿这些实例进行讲解。
基于大家基本都用Windows XP SP3,我也就在这个系统上把程序调试成功后再给大家讲解。
编程环境,我还是喜欢Visual C++ 6.0本书计划从四个大的方面来讲,这四个方面是:窗口类、文件操作类、网络类、数据库类。
都是时下流行的编程必备技术,也是软件开发者,必须掌握的技术。
中间以实例讲解,逐步学习,相信大家看完后会有很大的提高的。
第一章窗口类程序的编写这一章就先来讲解下窗口类程序的编写。
本系统基于客户端/服务器基本原理,程序即是服务端,也是客户端,通过IP就可以相互添加好友,并且实现点到点通信,有聊天记录(未实现根据用户发送的消息而改变字体颜色,只能在客户端改),字体颜色设置(一改全改~),包含主界面MaindBord和聊天Talk两大功能模块。
(1) MaindBord模块A、登录模块登陆模块的实现过程有下面几个步骤:(1)初始化列表信息,获取用户名,服务器的IP地址,使按键失效(2)确认登陆后创建套接字,绑定,监听(3)初始化成功后,激活添加好友和隐藏功能B、好友模块添加好友的实现过程有下面几个步骤:(1)弹出对话框(2)获取对话框中的IP(3)检查对方是否在线,建立连接(4)发送请求验证C、好友列表模块(1)客户接收请求,将服务端加入列表,发送客户端信息(2)服务端得到客户端信息,加入列表(3)双击好友列表,弹出发送信息对话框D、隐藏模块(1)隐藏与显示(2)响应鼠标,弹出控制菜单(2)聊天Talk模块A、发送信息模块聊天模块中,发送信息模块的实现过程有下面几个步骤:(1)通过列表双击一个好友为聊天对象,弹出聊天窗口;(2)用户按下发送按钮后,检测对方是否在线,检测对方是否在线(3)将聊天内容发送出去(4)在本地即时聊天内容显示的编辑框中添加发送的内容并写入聊天记录。
B、接收信息模块聊天模块中,接收信息模块的实现过程有下面几个步骤:(1)由主界面判断接收到信息的类型,是否为好友发来信息,如果是,则打开聊天窗口(2)读取消息,并写入聊天记录,(3)载入字体颜色信息。
(4) 显示聊天记录C、聊天记录管理模块聊天记录管理模块的实现过程有下面几个步骤:(1)将发送内容和接收内容写入文本文件;(2)显示聊天记录;(3)删除聊天记录。
D、字体颜色模块实现字体颜色模块的过程有下面几个步骤:(1)调用系统字体,获取字体,写入存档(2) 调用系统字体颜色,获取颜色,写入存档(3)读取、更新字体颜色。
让我们先看下运行结果:Windows7Windows2003Windows7 Windows2003Windows7 Windows2003Windows7Windows 2003心动了么?让我们开始对第一个模块,也就是MainBord的设计吧。
一、MainBord模块新建一个MainBord工程选择Dialog based,然后Finsh二、界面设计如下:(一)、MainBord界面设计如下:将各ID值设置如下图,通过类向导添加listbox成员变量(二)、添加好友对话框界面设计如下:双击,添加类将各ID值设置如下图,通过类向导添加成员变量(三)、弹出菜单界面设计如下:(四)添加图标三、代码部分:(一)准备部分首先,在StdAfx.h中添加如下代码:afxsock.h 头文件;在MainBordDlg.cpp添加Message.h 、AddFreind.h;其次,MainBordDlg.h添加如下代码:#define WM_CLIENT_ACCEPT WM_USER+101#define WM_CLIENT_READCLOSE WM_USER+102/////////////////////////////////////////////////////////////////////////////// CMainBordDlg dialog// Constructionpublic:int TrayAddIcon();char lpszTipBefore[100]; //修改前的图标提示信息LRESULT OnAccept(WPARAM wParam,LPARAM lParam);LRESULT OnRead(WPARAM wParam,LPARAM lParam);CMainBordDlg(CWnd* pParent = NULL); // standard constructorSOCKET Client; //客户的连接请求SOCKET ServerSocket; //SOCKETSOCKET tempSocket;SOCKADDR_IN tempSocketAddr;SOCKADDR_IN m_sockServerAddr; //SOCKET 结构BOOL IsTrue;CString ip;CString SystemMessage;void SendMsg();CString IPName;CString HostName;void GetIP_Name();void SelectWay();//好友列表struct FriendList{char name[100];char IP[18];struct FriendList *next;public:CString ip;} *start,*last;第三,添加Message.h文件代码如下:struct Message{int sign;char msg[1000];}msg;第四,添加消息响应LRESULT CMainBordDlg::OnRead(WPARAM wParam, LPARAM lParam) {if(!IsTrue){//m_List.InsertString(0,"有数据到达,但是没有工作站连接。
");}CString str;switch (WSAGETSELECTEVENT(lParam)){case FD_READ:if(recv(Client,(char *)&msg,sizeof(msg),0) == SOCKET_ERROR){// m_List.InsertString(0,"接收数据发生错误。
");return 0;}SelectWay();//m_History_List.InsertString(0,str);//MessageBox(str);break;case FD_CLOSE:str = _T("工作站退出。
");//m_List.InsertString(0,str);closesocket(Client);//IsTrue = FALSE;break;}return 0L;}LRESULT CMainBordDlg::OnAccept(WPARAM wParam, LPARAM lParam){if (WSAGETSELECTERROR(lParam)){//m_List.InsertString(0,"Error detected on entry into OnServerAccept.");return 0L;}if(WSAGETSELECTEVENT(lParam) == FD_ACCEPT)//如果{Client = accept(ServerSocket,(LPSOCKADDR)&m_sockServerAddr,0);if (Client == INV ALID_SOCKET){//m_List.InsertString(0,"Server socket failed to accept connection.");return 0L;}//ClinetWSAAsyncSelect(Client,m_hWnd,WM_CLIENT_READCLOSE,FD_READ|FD_CLOSE);IsTrue = TRUE;}//m_List.InsertString(0,"有客户机连接上了服务器。
");return 0L;}(一)、登录模块登陆模块的实现过程有下面几个步骤:(1)初始化列表,获取用户名,服务器的IP地址,使按键失效。
在MainBordDlg.cpp的初始化函数OnInitDialog添加如下代码:m_FriendList.SetItemHeight( 0, 18 );start=NULL;last=NULL;// TODO: Add extra initialization hereGetIP_Name();strcpy(lpszTipBefore,"QQ "+IPName);(CEdit *)GetDlgItem(IDC_AddBTN)->EnableWindow(FALSE);(CEdit *)GetDlgItem(IDC_HIDEBTN)->EnableWindow(FALSE);(2)在MainBordDlg.cpp添加获取主机获得IP和主机名的函数void CMainBordDlg::GetIP_Name()//获得IP和主机名{WORD wVersionRequested;WSADATA wsaData;char hostname[128];CString strip;PHOSTENT hostinfo;wVersionRequested=MAKEWORD(2,0);if(WSAStartup(wVersionRequested,&wsaData)==0){if(gethostname(hostname,sizeof(hostname))==0){if((hostinfo=gethostbyname(hostname))!=NULL){strip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);}}WSACleanup();}IPName=strip;HostName.Format("%s",hostname);}(3) 确定登陆后创建套接字,绑定,监听, 初始化成功后,激活添加好友和隐藏功能在MainBordDlg.cpp添加登陆按钮的方法void CMainBordDlg::OnStartBTN(){// TODO: Add your control notification handler code hereWSADATA wsaData;int iErrorCode;if (WSAStartup(MAKEWORD(2,1),&wsaData)) //调用Windows Sockets DLL{WSACleanup();return;}//m_List.InsertString(0,"服务器开始创建SOCKET。
");ServerSocket=socket(PF_INET,SOCK_STREAM,0); //创建服务器端Socket,类型为SOCK_STREAM,面向连接的通信if(ServerSocket == INVALID_SOCKET){//m_List.InsertString(0,"无法创建服务器socket!");return;}m_sockServerAddr.sin_family = AF_INET;m_sockServerAddr.sin_addr.s_addr = INADDR_ANY; //向所有的IP地址发送消息m_sockServerAddr.sin_port = htons(8001);if (bind(ServerSocket,(LPSOCKADDR)&m_sockServerAddr,sizeof(m_sockServerAddr)) == SOCKET_ERROR) //与选定的端口绑定{//m_List.InsertString(0,"无法绑定服务器。