2022年网络课程设计报告 .pdf
1 计算机网络课程设计报告学院:计算机科学学院专业:计算机科学与技术班级:姓名:学号:小组成员:2011-7-13 项目内容:基于 Socket 的文件传输服务设计与实现名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 13 页 - - - - - - - - - 2 实验目的:基于Socket套接口,实现文件数据传输服务。目的使学生掌握网络文件传输服务的设计方法。实验环境:操作系统: windowsXP 或 windows7;内存: 256M以上; Microsoft Visual C+ 6.0 设计方案:文件传送是各种计算机网络实现的基本功能,文件传送协议是一种最基本的应用层协议按照客户/服务器的模式进行工作,提供交互式的访问,是INTERNET使用最广泛的协议之一。 文件传输协议的简单设计与实现建立在计算机网络实验环境TCP/IP 网络体系结构之上,使用socket 编程接口编写两个程序,分别为客户程序(client.c) 和服务器程序(server.c)首先,我们知道此应用软件需实现网络中多台主机的信息互通,实现文件传输,因此涉及到主机网络互联的问题,所以必须会应用到网络协议,可以用 UDP或 TCP 。利用 IP地址接受文件内容。实现流程: 启动电脑,打开能运行该程序的环境,必须保证代码的正确性;进行窗体框架的设计,实现网络连接,并达到文件传输的功能;在以上步骤的成功进行下达到设计要求的基于Soc k et s的 局 域 网 内 文件 传 输 的 函 数 实 现 的 目 的 。源程序;程序清单:服务器:#include stdafx.h #include Server.h 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 13 页 - - - - - - - - - 3 #include ServerDlg.h #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE = _FILE_; #endif / / CServerDlg dialog CServerDlg:CServerDlg(CWnd* pParent /*=NULL*/) : CDialog(CServerDlg:IDD, pParent) /AFX_DATA_INIT(CServerDlg) / 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 CServerDlg:DoDataExchange(CDataExchange* pDX) CDialog:DoDataExchange(pDX); /AFX_DATA_MAP(CServerDlg) / NOTE: the ClassWizard will add DDX and DDV calls here /AFX_DATA_MAP BEGIN_MESSAGE_MAP(CServerDlg, CDialog) /AFX_MSG_MAP(CServerDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_LICSEN, OnButtonLicsen) ON_BN_CLICKED(IDC_BUTTON1, OnButtonOK) /AFX_MSG_MAP END_MESSAGE_MAP() / / CServerDlg message handlers BOOL CServerDlg:OnInitDialog() CDialog:OnInitDialog(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 13 页 - - - - - - - - - 4 / Set the icon for this dialog. The framework does this automatically / when the applications 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 return TRUE; / return TRUE unless you set the focus to a control / 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 CServerDlg:OnPaint() if (IsIconic() CPaintDC dc(this); / device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); / Center icon in client rectangle 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; / Draw the icon dc.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 CServerDlg:OnQueryDragIcon() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 13 页 - - - - - - - - - 5 return (HCURSOR) m_hIcon; void CServerDlg:OnButtonLicsen() / TODO: Add your control notification handler code here CFileDialog Dlg(TRUE); if(Dlg.DoModal()!=IDOK) return; CFile myFile; if(!myFile.Open(Dlg.GetPathName(), CFile:modeRead | CFile:typeBinary) AfxMessageBox( 文件不存在 !,MB_OK|MB_ICONERROR); return; CSocket sockSrvr; sockSrvr.Create(800); sockSrvr.Listen(); CSocket sockRecv; sockSrvr.Accept(sockRecv); SOCKET_STREAM_FILE_INFO StreamFileInfo; WIN32_FIND_DATA FindFileData; FindClose(FindFirstFile(Dlg.GetPathName(),&FindFileData); memset(&StreamFileInfo,0,sizeof(SOCKET_STREAM_FILE_INFO); strcpy(StreamFileInfo.szFileTitle,myFile.GetFileTitle(); StreamFileInfo.dwFileAttributes = FindFileData.dwFileAttributes; StreamFileInfo.ftCreationTime = FindFileData.ftCreationTime; StreamFileInfo.ftLastAccessTime = FindFileData.ftLastAccessTime; StreamFileInfo.ftLastWriteTime = FindFileData.ftLastWriteTime; StreamFileInfo.nFileSizeHigh = FindFileData.nFileSizeHigh; StreamFileInfo.nFileSizeLow = FindFileData.nFileSizeLow; sockRecv.Send(&StreamFileInfo,sizeof(SOCKET_STREAM_FILE_INFO); UINT dwRead=0; while(dwReadLoadIcon(IDR_MAINFRAME); void CClientDlg:DoDataExchange(CDataExchange* pDX) CDialog:DoDataExchange(pDX); /AFX_DATA_MAP(CClientDlg) / NOTE: the ClassWizard will add DDX and DDV calls here /AFX_DATA_MAP BEGIN_MESSAGE_MAP(CClientDlg, CDialog) /AFX_MSG_MAP(CClientDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend) ON_EN_CHANGE(IDC_EDIT_IPADDRESS, OnChangeEditIpaddress) ON_BN_CLICKED(IDC_BUTTON1, OnButtonOK) /AFX_MSG_MAP END_MESSAGE_MAP() / / CClientDlg message handlers 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 13 页 - - - - - - - - - 8 BOOL CClientDlg:OnInitDialog() CDialog:OnInitDialog(); / Set the icon for this dialog. The framework does this automatically / when the applications 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 return TRUE; / return TRUE unless you set the focus to a control / 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 CClientDlg:OnPaint() if (IsIconic() CPaintDC dc(this); / device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); / Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 13 页 - - - - - - - - - 9 CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; / Draw the icon dc.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 CClientDlg:OnQueryDragIcon() return (HCURSOR) m_hIcon; void CClientDlg:OnButtonSend() / TODO: Add your control notification handler code here AfxSocketInit(NULL); CSocket sockClient; sockClient.Create(); CString szIP; GetDlgItemText(IDC_EDIT_IPADDRESS,szIP); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 13 页 - - - - - - - - - 10 if(!sockClient.Connect(LPCTSTR)szIP, 800) AfxMessageBox( 连接到对方机器失败!); return; SOCKET_STREAM_FILE_INFO StreamFileInfo; sockClient.Receive(&StreamFileInfo,sizeof(SOCKET_STREAM_FILE_INFO); CFile destFile(StreamFileInfo.szFileTitle, CFile:modeCreate | CFile:modeWrite | CFile:typeBinary); UINT dwRead = 0; while(dwReadEnableWindow(FALSE); else GetDlgItem(IDC_BUTTON_SEND)-EnableWindow(TRUE); void CClientDlg:OnButtonOK() / TODO: Add your control notification handler code here OnOK(); 安装演示说明详细步骤 :名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 13 页 - - - - - - - - - 12 服务器向客户机发送文件:效果如图客户器输入服务器的IP 地址并选择接受来自服务器发送的文件,选择接受。效果如下图:输入 IP 地址如果地址有误则显示:实验总结:通过本次计算机网络课程设计,我更加充分的理解了课本上的知识,并能够加以扩展,从而应用于实践当中,这几天的课程设计令我受益匪浅,很多平时模棱两可的知识点都认真复习并实践了。我意识到我们所学的东西将来都是要付诸实践的,所以一切要名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 13 页 - - - - - - - - - 13 从实际情况出发,理论联系实际, 这样才能真正发挥我们所具备的能力。经过此次课程设计,我向我成功的目标又迈进了一步名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 13 页 - - - - - - - - -