Windows网络编程课程设计.docx

上传人:b****6 文档编号:8420754 上传时间:2023-01-31 格式:DOCX 页数:17 大小:102.96KB
下载 相关 举报
Windows网络编程课程设计.docx_第1页
第1页 / 共17页
Windows网络编程课程设计.docx_第2页
第2页 / 共17页
Windows网络编程课程设计.docx_第3页
第3页 / 共17页
Windows网络编程课程设计.docx_第4页
第4页 / 共17页
Windows网络编程课程设计.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

Windows网络编程课程设计.docx

《Windows网络编程课程设计.docx》由会员分享,可在线阅读,更多相关《Windows网络编程课程设计.docx(17页珍藏版)》请在冰豆网上搜索。

Windows网络编程课程设计.docx

Windows网络编程课程设计

Windows网络程序设计

 

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

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

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

2

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

3

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

.12

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

14

 

1。

设计目的及意义。

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

2。

简单的需求分析。

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

3.系统的总体设计。

本系统流程图为:

 

4。

系统功能模块及关键代码

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

classCPortSetDlg:

publicCDialog

//Construction

public:

CPortSetDlg(CWnd*pParent=NULL);//standardconstructor

//DialogData

//{{AFX_DATA(CPortSetDlg)

enum{IDD=IDD_DIALOG1};

UINTm_PortSet;

//}}AFX_DATA

 

//Overrides

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CPortSetDlg)

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport

//}}AFX_VIRTUAL

//Implementation

protected:

//Generatedmessagemapfunctions

//{{AFX_MSG(CPortSetDlg)

//NOTE:

theClassWizardwilladdmemberfunctionshere

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

服务器端口设置好后,系统就会自动弹出一个对话框提示“服务器端口设置ok!

”,点“确定”后,就会进入服务器端主界面。

主界面主要有有个输入框,及4个功能模块:

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

voidCBCServerDlg:

:

OnPortSet()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

CPortSetDlgdlg;

if(dlg.DoModal()==IDOK)

m_pSocket=newCListenSocket;

if(m_pSocket—>Create(dlg。

m_PortSet))

if(!

m_pSocket->Listen())

MessageBox("服务器端口设置错误!

","服务器端口设置”);

else

MessageBox(”服务器端口设置ok!

",”服务器端口设置”);

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

voidCBCServerDlg:

:

OnOK()

{

//TODO:

Addextravalidationhere

if(!

m_SocketList。

IsEmpty())

UpdateData();

POSITIONpos;

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());

}

}

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

//CDialog:

:

OnOK();

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

voidCBCServerDlg:

OnClearOut()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

m_OutInfo。

Empty();

UpdateData(false);

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

voidCBCServerDlg:

OnCancel()

{

//TODO:

Addextracleanuphere

if(m_pSocket)

{deletem_pSocket;}

m_SocketList.RemoveAll();

CDialog:

OnCancel();

 

CDialog:

:

OnCancel();

}

其中有个与用户连接是否成功的判断提示代码为:

voidCBCServerDlg:

:

OnOK()

{

//TODO:

Addextravalidationhere

if(!

m_SocketList.IsEmpty())

UpdateData();

POSITIONpos;

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());

}

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

//CDialog:

OnOK();

voidCClientSocket:

OnReceive(intnErrorCode)

{

//TODO:

Addyourspecializedcodehereand/orcallthebaseclass

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

CSocket:

:

OnReceive(nErrorCode);

}

classCBCServerDlg:

publicCDialog

//Construction

public:

CBCServerDlg(CWnd*pParent=NULL);//standardconstructor

voidGetSocketMsg(CClientSocket*pSocket);

CPtrListm_SocketList;

CListenSocket*m_pSocket;

//DialogData

//{{AFX_DATA(CBCServerDlg)

enum{IDD=IDD_BCServer_DIALOG};

CStringm_OutInfo;

//}}AFX_DATA

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CBCServerDlg)

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport

//}}AFX_VIRTUAL

//Implementation

protected:

HICONm_hIcon;

//Generatedmessagemapfunctions

//{{AFX_MSG(CBCServerDlg)

virtualBOOLOnInitDialog();

afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);

afx_msgvoidOnPaint();

afx_msgHCURSOROnQueryDragIcon();

afx_msgvoidOnPortSet();

//afx_msgvoidOnClearIn();

afx_msgvoidOnClearOut();

virtualvoidOnCancel();

virtualvoidOnOK();

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

程序运行开始时,先初始化服务器端程序主要代码是:

BOOLCBCServerDlg:

:

OnInitDialog()

CDialog:

OnInitDialog();

//Add"About。

”menuitemtosystemmenu。

//IDM_ABOUTBOXmustbeinthesystemcommandrange。

ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);

ASSERT(IDM_ABOUTBOX〈0xF000);

CMenu*pSysMenu=GetSystemMenu(FALSE);

if(pSysMenu!

=NULL)

CStringstrAboutMenu;

strAboutMenu。

LoadString(IDS_ABOUTBOX);

if(!

strAboutMenu.IsEmpty())

{

pSysMenu—>AppendMenu(MF_SEPARATOR);

pSysMenu—〉AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);

//Settheiconforthisdialog.Theframeworkdoesthisautomatically

//whentheapplication’smainwindowisnotadialog

SetIcon(m_hIcon,TRUE);//Setbigicon

SetIcon(m_hIcon,FALSE);//Setsmallicon

//TODO:

Addextrainitializationhere

OnPortSet();

returnTRUE;//returnTRUEunlessyousetthefocustoacontrol

}

voidCBCServerDlg:

OnSysCommand(UINTnID,LPARAMlParam)

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

CAboutDlgdlgAbout;

dlgAbout。

DoModal();

}

else

CDialog:

OnSysCommand(nID,lParam);

}

classCClientUseSocket:

publicCSocket

{

//Attributes

public:

//Operations

public:

CClientUseSocket();

virtual~CClientUseSocket();

//Overrides

public:

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CClientUseSocket)

public:

virtualvoidOnReceive(intnErrorCode);

//}}AFX_VIRTUAL

//Generatedmessagemapfunctions

//{{AFX_MSG(CClientUseSocket)

//NOTE-theClassWizardwilladdandremovememberfunctionshere。

//}}AFX_MSG

//Implementation

protected:

};

用户端程序主要有2个功能模块.

(1)连接服务器,点击后,会弹出一个输入服务器IP地址,和服务器端号.主要代码为:

voidCUseSocketDlg:

OnConnect()

//TODO:

Addyourcontrolnotificationhandlercodehere

CConnectDlgdlg;

if(dlg。

DoModal()==IDOK)

m_ServerName=dlg。

m_ServerName;

m_PortAddress=dlg。

m_PortAddress;

m_pSocket=newCClientUseSocket();

if(!

(ConnectServer(m_pSocket)))

m_pSocket=NULL;

return;

else

{

MessageBox("Socket连接成功!

”,"userprogram");

}

voidCUseSocketDlg:

:

OnDisconnect()

//TODO:

Addyourcontrolnotificationhandlercodehere

if(m_pSocket)

deletem_pSocket;

m_pSocket=NULL;

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

}

else

MessageBox(”当前未连接到任何服务器!

",”提示”);

voidCUseSocketDlg:

:

OnConceal()

//TODO:

Addyourcontrolnotificationhandlercodehere

if(m_pSocket)

deletem_pSocket;

}

CDialog:

:

OnCancel();

}

classCUseSocketApp:

publicCWinApp

public:

CUseSocketApp();

//Overrides

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CUseSocketApp)

public:

virtualBOOLInitInstance();

//}}AFX_VIRTUAL

//Implementation

//{{AFX_MSG(CUseSocketApp)

//NOTE-theClassWizardwilladdandremovememberfunctionshere。

//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

voidCUseSocketDlg:

:

GetSocketMsg(CClientUseSocket*pSocket)

BOOLMsgEnd=false;

CStringMsg="收到服务器消息:

”;

charMsgBuf[100];

intbufsize=100;

do

{

strcpy(MsgBuf,"”);

intleft=pSocket-〉Receive(MsgBuf,bufsize);

if(left<100&&left〉0)

MsgEnd=true;

MsgBuf[left]=0;

Msg+=MsgBuf;

while(!

MsgEnd);

MessageBox(Msg,"userprogram”);

}

BOOLCUseSocketDlg:

ConnectServer(CClientUseSocket*pSocket)

if(!

(pSocket-〉Create()))

deletepSocket;

pSocket=NULL;

MessageBox("socket创建不成功”,"提示");

returnfalse;

}

if(!

(pSocket—〉Connect(m_ServerName,m_PortAddress)))

deletepSocket;

pSocket=NULL;

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

returnfalse;

returntrue;

}

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

voidCUseSocketDlg:

OnDisconnect()

//TODO:

Addyourcontrolnotificationhandlercodehere

if(m_pSocket)

{

deletem_pSocket;

m_pSocket=NULL;

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

}

else

MessageBox(”当前未连接到任何服务器!

",”提示");

classCConnectDlg:

publicCDialog

{

//Construction

public:

CConnectDlg(CWnd*pParent=NULL);//standardconstructor

//DialogData

//{{AFX_DATA(CConnectDlg)

enum{IDD=IDD_DIALOG1};

UINTm_PortAddress;

CStringm_ServerName;

//}}AFX_DATA

 

//Overrides

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CConnectDlg)

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport

//}}AFX_VIRTUAL

//Implementation

protected:

//Generatedmessagemapfunctions

//{{AFX_MSG(CConnectDlg)

//NOTE:

theClassWizardwilladdmemberfunctionshere

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

voidCUseSocketDlg:

:

OnConceal()

//TODO:

Addyourcontrolnotificationhandlercodehere

if(m_pSocket)

deletem_pSocket;

CDialog:

:

OnCancel();

5。

调试与测试

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

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

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

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

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

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

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

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

 

6。

设计体会

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

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 工学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1