《计算机网络课程设计报告》
学院:计算机科学学院 专业:计算机科学与技术 班级: 姓名: 学号:
小组成员:
2011-7-13
项目内容:基于Socket的文件传输服务设计与实现
实验目的:基于Socket套接口,实现文件数据传输服务。目的使学生掌握网络文件传输服务的设计方法。
实验环境:操作系统:windowsXP或windows7;
内存:256M以上; Microsoft Visual C++ 6.0
设计方案:文件传送是各种计算机网络实现的基本功能,文件传送协议是一种最基本的应用层协议按照客户/服务器的模式进行工作,提供交互式的访问,是INTERNET使用最广泛的协议之一。文件传输协议的简单设计与实现建立在计算机网络实验环境TCP/IP 网络体系结构之上,使用socket 编程接口编写两个程序,分别为客户程序(client.c)和服务器程序(server.c)首先,我们知道此应用软件需实现网络中多台主机的信息互通,实现文件传输,因此涉及到主机网络互联的问题,所以必须会应用到网络协议,可以用UDP或TCP。利用IP地址接受文件内容。
实现流程: 启动电脑,打开能运行该程序的环境,必须保证代码的正确性;
进行窗体框架的设计,实现网络连接,并达到文件传输的功能;
在以上步骤的成功进行下达到设计要求的基于Sockets的局域网内文件传输的函数实现的目的。
源程序;
程序清单: 服务器:
#include \"stdafx.h\" #include \"Server.h\"
#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 ClaWizard 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 ClaWizard 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 meage handlers
BOOL CServerDlg::OnInitDialog() { CDialog::OnInitDialog(); 3
// 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
return TRUE; // return TRUE unle 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
SendMeage(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()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
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)) {
AfxMeageBox(\"文件不存在!\",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.ftLastAcceTime = FindFileData.ftLastAcceTime; StreamFileInfo.ftLastWriteTime = FindFileData.ftLastWriteTime; StreamFileInfo.nFileSizeHigh = FindFileData.nFileSizeHigh; StreamFileInfo.nFileSizeLow = FindFileData.nFileSizeLow;
sockRecv.Send(&StreamFileInfo,sizeof(SOCKET_STREAM_FILE_INFO));
UINT dwRead=0; while(dwRead
byte* data = new byte[1024]; 5
UINT dw=myFile.Read(data, 1024);
sockRecv.Send(data, dw);
dwRead+=dw; } myFile.Close();
sockRecv.Close(); AfxMeageBox(\"发送完毕!\"); }
void CServerDlg::OnButtonOK() { // TODO: Add your control notification handler code here OnOK();
} 客户机:
// ClientDlg.cpp : implementation file // Download by http://www.codefans.net
#include \"stdafx.h\" #include \"Client.h\" #include \"ClientDlg.h\"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
///////////////////////////////////////////////////////////////////////////// // CClientDlg dialog
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/) 6
: CDialog(CClientDlg::IDD, pParent) { //{{AFX_DATA_INIT(CClientDlg) // NOTE: the ClaWizard 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 CClientDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CClientDlg) // NOTE: the ClaWizard 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, OnChangeEditIpaddre) ON_BN_CLICKED(IDC_BUTTON1, OnButtonOK) //}}AFX_MSG_MAP END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CClientDlg meage handlers
BOOL CClientDlg::OnInitDialog() { CDialog::OnInitDialog();
// 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); SetIcon(m_hIcon, FALSE);
// TODO: Add extra initialization here
return TRUE; // return TRUE unle 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()) {
// Set big icon // Set small icon CPaintDC dc(this); // device context for painting SendMeage(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()cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); else {
} }
// 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 CDialog::OnPaint();
if(!sockClient.Connect((LPCTSTR)szIP, 800)) {
AfxMeageBox(\"连接到对方机器失败!\");
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(dwRead
byte* data = new byte[1024];
memset(data,0,1024);
UINT dw=sockClient.Receive(data, 1024);
destFile.Write(data, dw);
dwRead+=dw; }
SetFileTime((HANDLE)destFile.m_hFile,&StreamFileInfo.ftCreationTime,
&StreamFileInfo.ftLastAcceTime,&StreamFileInfo.ftLastWriteTime); destFile.Close();
|
SetFileAttributes(StreamFileInfo.szFileTitle,StreamFileInfo.dwFileAttributes); sockClient.Close(); AfxMeageBox(\"接收完毕!\"); }
void CClientDlg::OnChangeEditIpaddre() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unle you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here CString szIpAddre;
GetDlgItemText(IDC_EDIT_IPADDRESS,szIpAddre); if(szIpAddre.IsEmpty()) GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE); else
}
void CClientDlg::OnButtonOK() { // TODO: Add your control notification handler code here OnOK(); } 安装演示说明详细步骤: GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE); 11
服务器向客户机发送文件:效果如图
客户器输入服务器的IP地址并选择接受来自服务器发送的文件,选择接受。效果如下图:
输入IP地址
如果地址有误则显示
:
实验总结:
通过本次计算机网络课程设计,我更加充分的理解了课本上的知识,并能够加以扩展,从而应用于实践当中,这几天的课程设计令我受益匪浅,很多平时模棱两可的知识点都认真复习并实践了。我意识到我们所学的东西将来都是要付诸实践的,所以一切要 12
从实际情况出发,理论联系实际,这样才能真正发挥我们所具备的能力。经过此次课程设计,我向我成功的目标又迈进了一步