计算机网络上机报告



《计算机网络上机报告》由会员分享,可在线阅读,更多相关《计算机网络上机报告(65页珍藏版)》请在文档大全上搜索。
1、实验一 聊天程序一、实验目的1、熟悉VisualC+的基本操作(编译,运行,检错等);(2)基本了解基于对话框的Windows应用程序的编写过程;2、能够熟练的使用例程建立客户端和用户端之间的通信连接,并能发送和接收消息;二、实验要求1、Server端在侦听之后,可以等待并接受来自聊天客户(Client端)的连接请求,同时显示连接成功的对话框;2、Client端可以通过输入Server端的IP地址进行连接操作;3、在建立连接之后,Client端和Server端可以相互发送信息并显示在屏幕上;三、实验原理1、服务器端和客户端。服务器端用于提供一个网络端口,等待客户端发出请求,建立连接,然后进行网
2、络通讯和消息的转发;客户端可通过服务器端的IP地址发送连接请求。服务端和客户端相互发送消息。2、本实验既可以进行本机操作而且不同机器之间的聊天。四、实验步骤1、本机与本机连接:(1)运行client,server程序,建立连接。首先进行本机聊天操作,显示连接成功,得到以下结果。(lx1表示client端,lx2表示sever端)。(2)lx1发送“13040310017蒲昭福”,lx2收到。(3)lx2发送“ok,收到!”,lx1收到。2、不同机器之间进行连接:在两台电脑上运行ipconfig指令,得到本机ip地址和另外台电脑的ip地址。(1)本机为client端,另外台为server端:另一
3、台打开server.exe并点击“侦听”,然后本机打开client.exe并输入对方的ip地址,点击“连接”。(2)本机为server端,另外台为client端:本机打开server.exe并点击“侦听”,然后另一台打开client.exe并输入对方的ip地址,点击“连接”。五、核心源代码Client:#include "stdafx.h"#include "lx1.h"#include "Client.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char TH
4、IS_FILE = _FILE_;#endif#include "lx1Dlg.h"/ CClientCClient:CClient()CClient:CClient()/ Do not edit the following lines, which are needed by ClassWizard.#if 0BEGIN_MESSAGE_MAP(CClient, CSocket)/AFX_MSG_MAP(CClient)/AFX_MSG_MAPEND_MESSAGE_MAP()#endif/ 0/ CClient member functionsvoid CClient:
5、OnReceive(int nErrorCode) / TODO: Add your specialized code here and/or call the base class(CLx1Dlg*)(AfxGetApp()->m_pMainWnd)->ShowMsg();CSocket:OnReceive(nErrorCode);Server:#include "stdafx.h"#include "lx2.h"#include "Server.h"#ifdef _DEBUG#define new DEBUG_NEW#
6、undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif#include "lx2Dlg.h"/ CServerCServer:CServer()CServer:CServer()/ Do not edit the following lines, which are needed by ClassWizard.#if 0BEGIN_MESSAGE_MAP(CServer, CSocket)/AFX_MSG_MAP(CServer)/AFX_MSG_MAPEND_MESSAGE_MAP()#endif/ 0/ CServer
7、member functionsvoid CServer:OnAccept(int nErrorCode)/ TODO: Add your specialized code here and/or call the base classCSocket:OnAccept(nErrorCode);(CLx2Dlg*)(AfxGetApp()->m_pMainWnd)->ShowAccept();void CServer:OnReceive(int nErrorCode) / TODO: Add your specialized code here and/or call the bas
8、e classCSocket:OnReceive(nErrorCode);(CLx2Dlg*)(AfxGetApp()->m_pMainWnd)->ShowMsg();实验二 网络文件传输一、实验目的1、熟悉TCP/IP协议的功能和网络操作2、编写小型的socket应用程序,初步了解Windows环境下socket编程,实现点对点文本传输。二、实验要求实现在本机上传输文件。三、实验原理本程序是基于TCP/IP协议栈的网络编程,使用C语言,利用操作系统提供的套接字网络编程接口进行编写。TCP协议是其中重要的传输协议,为应用层提供可靠的端到端的流传输服务.其中套接字取值sock_str
9、eam即为TCP的流传输服务。四、实验步骤1、文件传输单线程1:运行client,server 程序,得到以下结果2、文件传输单线程2:运行client,server 程序,得到以下结果3、文件传输多线程:运行client,server 程序,得到以下结果五、核心源代码1、文件传输单线程1:Client:#include "stdafx.h"#include "Server.h"#include "ServerDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic
10、char THIS_FILE = _FILE_;#endif/ CServerAppBEGIN_MESSAGE_MAP(CServerApp, CWinApp)/AFX_MSG_MAP(CServerApp)/ 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()/ CServerA
11、pp constructionCServerApp:CServerApp()/ TODO: add construction code here,/ Place all significant initialization in InitInstance/ The one and only CServerApp objectCServerApp theApp;/ CServerApp initializationBOOL CServerApp:InitInstance()if (!AfxSocketInit()AfxMessageBox(IDP_SOCKETS_INIT_FAILED);ret