您的当前位置:首页Windows网络编程课程设计

Windows网络编程课程设计

来源:小侦探旅游网


目录

1. 设计目的及意义 ………………………………………………………………………2

2. 简单的需求分析 ………………………………………………………………………2

3. 系统总体设计 …………………………………………………………………….。2

4. 系统功能模块及关键代码 …………………………………………………………。3

5. 调试与测试 …………………………………………………………………………。.12

6. 设计体会 ……………………………………………………………………………。14

1。设计目的及意义。

本局域网消息广播系统的设计开发,目的主要是为一个局域网中的服务器向所有主机发送消息,目前公司中一般都有比较多的电脑,假如一台管理员的电脑想向所有的其它员工电脑发送消息的话,如果通过每次只向一个员工电脑发送消息的话,那就要发送很多次了,这样就浪费有很多时间,本系统就是基于一个这样的目的才开发的,只要管理员电脑运行本系统服务器程序,员工电脑运行客户端程序,只要管理员在电脑里输入一次要发送给员工的信息,所有的员工都能马上收到发来的信息,本系统的意义在于为企业,公司等有多台电脑管理的部门提供了方便,不必重复输入相同信息,也不必多次发送相同内容,节约了大量的时间。

2。简单的需求分析。

目前很多公司,企业及学校等等一般都有比较多的电脑,员工工作时,部门经理也经常会与员工交流工作中的一些问题,也会向员工发布一些通知,如果向每台员工电脑都发送一次相同的信息,就会多次重复的输入,重复发送,这样就会降低工作效率,从而本局域网消息广播系统的应用还是比较大的,基本上目前每个公司,企业和学校都会使用与本系统相关的系统来提高工作效率。

3.系统的总体设计。

本系统流程图为:

局域网消息广播 服务器端 用户端 设置端口 发送消息 清除消息 关闭 连接服务器断开连接

4。系统功能模块及关键代码

- 1 -

首先服务器端程序运行后,就会弹出“端口设置对话框”,提示输入服务器端的端口号,这个功能是由CPortSetDlg对话框类实现的,通过继承windows类实现,主要代码如下:

class CPortSetDlg : public CDialog {

// Construction public:

CPortSetDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data //{{AFX_DATA(CPortSetDlg) enum { IDD = IDD_DIALOG1 }; UINT m_PortSet; //}}AFX_DATA

// Overrides

// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CPortSetDlg) protected:

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

//}}AFX_VIRTUAL

// Implementation protected:

// Generated message map functions //{{AFX_MSG(CPortSetDlg)

// NOTE: the ClassWizard will add member functions here //}}AFX_MSG

DECLARE_MESSAGE_MAP() };

服务器端口设置好后,系统就会自动弹出一个对话框提示“服务器端口设置ok!”,点“确定”后,就会进入服务器端主界面。主界面主要有有个输入框,及4个功能模块:

(1)设置端口功能模块,主要是用于设置服务器端口号,主要代码如下: void CBCServerDlg::OnPortSet() {

// TODO: Add your control notification handler code here

CPortSetDlg dlg;

if(dlg.DoModal()==IDOK) {

- 2 -

m_pSocket=new CListenSocket;

if(m_pSocket—>Create(dlg。m_PortSet)) {

if(!m_pSocket->Listen())

MessageBox(\"服务器端口设置错误!\服务器端口设置”); else

MessageBox(”服务器端口设置ok!\”服务器端口设置”); } } }

(2)发送消息功能模块主要代码: void CBCServerDlg::OnOK() {

// TODO: Add extra validation here if(!m_SocketList。IsEmpty()) {

UpdateData(); POSITION pos;

CClientSocket * pSocket =(CClientSocket *) m_SocketList。GetHead();

for(pos=m_SocketList。GetHeadPosition();pos!=NULL;) {

pSocket=(CClientSocket * )m_SocketList.GetNext(pos);

pSocket—〉Send(LPCTSTR(m_OutInfo),m_OutInfo.GetLength()); } }

else MessageBox(”不能建立服务器连接,不能广播信息”,\"错误提示对话框\");

//CDialog::OnOK(); }

(3) 清除消息功能模块主要代码: void CBCServerDlg::OnClearOut() {

// TODO: Add your control notification handler code here m_OutInfo。Empty(); UpdateData(false); }

(4) 关闭服务器功能模块主要代码: void CBCServerDlg::OnCancel() {

// TODO: Add extra cleanup here if(m_pSocket)

- 3 -

{delete m_pSocket;}

m_SocketList.RemoveAll(); CDialog::OnCancel();

CDialog::OnCancel(); }

其中有个与用户连接是否成功的判断提示代码为: void CBCServerDlg::OnOK() {

// TODO: Add extra validation here if(!m_SocketList.IsEmpty()) {

UpdateData(); POSITION pos; CClientSocket * pSocket =(CClientSocket *) m_SocketList.GetHead();

for(pos=m_SocketList.GetHeadPosition();pos!=NULL;) {

pSocket=(CClientSocket * )m_SocketList。GetNext(pos);

pSocket-〉Send(LPCTSTR(m_OutInfo),m_OutInfo.GetLength()); } }

else MessageBox(\"不能建立服务器连接,不能广播信息\错误提示对话框”);

//CDialog::OnOK(); }

void CClientSocket::OnReceive(int nErrorCode) {

// TODO: Add your specialized code here and/or call the base class

((CBCServerDlg *)(AfxGetApp()->m_pMainWnd))—>GetSocketMsg(this);

CSocket::OnReceive(nErrorCode); }

class CBCServerDlg : public CDialog {

// Construction public:

- 4 -

CBCServerDlg(CWnd* pParent = NULL); // standard constructor

void GetSocketMsg(CClientSocket * pSocket);

CPtrList m_SocketList;

CListenSocket * m_pSocket;

// Dialog Data

//{{AFX_DATA(CBCServerDlg)

enum { IDD = IDD_BCServer_DIALOG }; CString m_OutInfo; //}}AFX_DATA

// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CBCServerDlg) protected:

virtual void DoDataExchange(CDataExchange* pDX); // support

//}}AFX_VIRTUAL

// Implementation protected:

HICON m_hIcon;

// Generated message map functions //{{AFX_MSG(CBCServerDlg) virtual BOOL OnInitDialog();

afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint();

afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnPortSet(); //afx_msg void OnClearIn(); afx_msg void OnClearOut(); virtual void OnCancel(); virtual void OnOK(); //}}AFX_MSG

DECLARE_MESSAGE_MAP() };

程序运行开始时,先初始化服务器端程序主要代码是: BOOL CBCServerDlg::OnInitDialog() {

CDialog::OnInitDialog();

- 5 -

DDX/DDV // 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 dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

OnPortSet();

return TRUE; // return TRUE unless you set the focus to a control }

void CBCServerDlg::OnSysCommand(UINT nID, LPARAM lParam) {

if ((nID & 0xFFF0) == IDM_ABOUTBOX) {

CAboutDlg dlgAbout; dlgAbout。DoModal(); } else {

CDialog::OnSysCommand(nID, lParam); } }

- 6 -

class CClientUseSocket : public CSocket {

// Attributes public:

// Operations public:

CClientUseSocket();

virtual ~CClientUseSocket();

// Overrides public:

// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CClientUseSocket) public:

virtual void OnReceive(int nErrorCode); //}}AFX_VIRTUAL

// Generated message map functions //{{AFX_MSG(CClientUseSocket)

// NOTE - the ClassWizard will add and remove member functions here。

//}}AFX_MSG

// Implementation protected: };

用户端程序主要有2个功能模块. (1)连接服务器,点击后,会弹出一个输入服务器IP地址,和服务器端号.主要代码为:

void CUseSocketDlg::OnConnect() {

// TODO: Add your control notification handler code here CConnectDlg dlg;

if(dlg。DoModal()==IDOK) {

m_ServerName=dlg。m_ServerName; m_PortAddress=dlg。m_PortAddress; m_pSocket=new CClientUseSocket(); if(!(ConnectServer(m_pSocket))) {

m_pSocket=NULL; return; }

- 7 -

else {

MessageBox(\"Socket连接成功!”,\"user program\"); } } }

void CUseSocketDlg::OnDisconnect() {

// TODO: Add your control notification handler code here if(m_pSocket) {

delete m_pSocket; m_pSocket=NULL;

MessageBox(\"Socket连接已经断开\",\"提示”); }

else

MessageBox(”当前未连接到任何服务器!\”提示”); }

void CUseSocketDlg::OnConceal() {

// TODO: Add your control notification handler code here if(m_pSocket) {

delete m_pSocket; }

CDialog::OnCancel(); }

class CUseSocketApp : public CWinApp {

public:

CUseSocketApp();

// Overrides

// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CUseSocketApp) public:

virtual BOOL InitInstance(); //}}AFX_VIRTUAL

// Implementation

//{{AFX_MSG(CUseSocketApp)

// NOTE - the ClassWizard will add and remove member functions here。

- 8 -

// DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG

DECLARE_MESSAGE_MAP() };

void CUseSocketDlg::GetSocketMsg(CClientUseSocket * pSocket) {

BOOL MsgEnd=false;

CString Msg=\"收到服务器消息:”;

char MsgBuf[100]; int bufsize=100; do {

strcpy(MsgBuf,\"”);

int left=pSocket-〉Receive(MsgBuf,bufsize); if(left<100&&left〉0) MsgEnd=true; MsgBuf[left]=0; Msg+=MsgBuf; }

while(!MsgEnd);

MessageBox(Msg,\"user program”); }

BOOL CUseSocketDlg::ConnectServer(CClientUseSocket * pSocket) {

if(!(pSocket-〉Create())) {

delete pSocket; pSocket=NULL;

MessageBox(\"socket创建不成功”,\"提示\"); return false; } if(!(pSocket—〉Connect(m_ServerName,m_PortAddress))) {

delete pSocket; pSocket=NULL;

MessageBox(”服务器连接失败”,”提示”); return false; }

return true; }

(2)用户端程序另一个功能模块是断开连接,主要代码是: void CUseSocketDlg::OnDisconnect() {

- 9 -

// TODO: Add your control notification handler code here if(m_pSocket) {

delete m_pSocket; m_pSocket=NULL;

MessageBox(\"Socket连接已经断开”,”提示\"); }

else

MessageBox(”当前未连接到任何服务器!\”提示\"); }

class CConnectDlg : public CDialog {

// Construction public:

CConnectDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data

//{{AFX_DATA(CConnectDlg) enum { IDD = IDD_DIALOG1 }; UINT m_PortAddress; CString m_ServerName; //}}AFX_DATA

// Overrides

// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CConnectDlg) protected:

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL

// Implementation protected:

// Generated message map functions //{{AFX_MSG(CConnectDlg)

// NOTE: the ClassWizard will add member functions here //}}AFX_MSG

DECLARE_MESSAGE_MAP() };

void CUseSocketDlg::OnConceal() {

// TODO: Add your control notification handler code here

- 10 -

if(m_pSocket) {

delete m_pSocket; }

CDialog::OnCancel(); }

5。调试与测试

(1) 运行服务器端程序,就会弹出“端口设置对话框”如下图:

(2)点“确定\"后,就会弹出提示对话框,如截图:

(3)点这个对话框的“确定”后,就进入服务器端主界面,如截图:

(4) 设置端口对话框用于设置服务器端口,“发送”功能用于服务器向全部客户发送信息,如截图:

- 11 -

这时运行客户端就会收到服务器发来的消息,如截图:

(5)如果,客户端设置的IP和端口与服务器不同,服务器不能与客户建立连接,就会弹出提示对话框,如截图:

(6)当运行用户端程序后,会弹出用户对话框,如截图:

- 12 -

(7)点击“连接服务器”后,会进入设置IP与端口对话框,如截图:

6。设计体会

通过本局域网消息广播系统的设计与开发,深深地让我们明白把理论用于实践

的重要性,本来对学计算机的同学来说,实践性是非常重要的,如果只注重计算机理论,而忽视它的运用与操作性,那么可能永远也不能真正的学会计算机.在开发本系统过程中,我们也发现了自己在本课程及计算机理论的一些知识上的不足,希望能通过这次课程设计的开发,来加强我们对理论知识的巩固,从而本次的系统的开发来引导我们进行相关知识的学习与扩展,总之,本系统的设计与开发,让我们实践能力提高了,也有了一次实践的经历,希望以后会通过更多的实践机会来提高编程开发能力。

- 13 -

因篇幅问题不能全部显示,请点此查看更多更全内容