TCPIP实验报告_第1页
TCPIP实验报告_第2页
TCPIP实验报告_第3页
TCPIP实验报告_第4页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、tcpip实验报告 tcp/ip 技术实验报告书 专 业:通 信 工 程 学生姓名:张 世 超 完成时间:2021 年 7 月 15 日 实验一 网络应用程序基础 实验目的: 通过实验,使学生熟悉并掌握运用 tcp/ip 技术进行网络编程的基本知识,加深对课堂教学内容的理解,掌握套接字网络通信编程技术,能够运用 vc+为开发工具编程解决网络通信中的实际问题,进行一些简单的网络应用程序设计。实验内容: 1,winsock 的启动与终止。 2,winsock 的创建及绑定和关闭。 3,建立通信连接 listen 及 accept 和 connect。 4,数据的传输。 5,简单的客户机/服务器之间

2、的通信。 要求:通过在 sdk 模式下完成数据通信的过程,掌握 windows socket 的常用函数的形式和使用方法,理解数据通信的过程。 实验步骤: 1,打开 vc 环境 1,使用向导为客户端创建工程:选择可执行程序,选择使用 wsa 环境,单文档环境,其他的选择默认设置 2,在文件中添加代码 3,编译调试 4,使用向导为服务器端创建工程:选择可执行程序,选择使用 wsa 环境,单文档环境,其他的选择默认设置 5,在文件中添加代码 6,编译调试 7,分别打开两个系统命令窗口中,并分别在其中运行客户端和服务器端程序。 8,在客户端侧输入字符,可以看到服务器收到字符串 参考代码:课本 156

3、 页-160 页 实验结果: client: #includewinsock2.h #includestdio.h /服务器端口号为 5050 #define default_port 5050 #define data_buffer 1024 void main(int argc,char *argv) wsadata wsadata; socket sclient; int iport=default_port; /从服务器端接收的数据长度 int ilen; /接收数据的缓冲 char bufdata_buffer; /服务器端地址 struct sockaddr_in ser; /判断

4、输入的参数是否正确 if(argc2) /提示在命令行中输入服务器 ip 地址 printf(usage:client server ip addressn); return; /接收数据的缓冲区初始化 memset(buf,0,sizeof(buf); if(wsastartup(makeword(2,2),wsadata)!=0) printf(failed to load winsock.n); return; /填写要连接的服务器地址信息 ser.sin_family=af_inet; ser.sin_port=htons(iport); /inet_addr()函数将命令行的点分ip

5、地址转换为用二进制表示的网络字节顺序的 ip 地址 ser.sin_addr.s_addr=inet_addr(argv1); /建立客户端流式套接口 sclient=socket(af_inet,sock_stream,0); if(sclient=invalid_socket) printf(socket() failed:%dn,wsagetlasterror(); return; /请求与服务器端建立 tcp 连接 if(connect(sclient,(struct sockaddr*)ser,sizeof(ser)=invalid_socket) printf(connect()

6、failed:%dn,wsagetlasterror(); return; else /从服务器端接收数据 ilen=recv(sclient,buf,sizeof(buf),0); if(ilen=0) return; else if(ilen=socket_error) printf(recv() failed:%d,wsagetlasterror(); return; printf(recv() data from server:%sn,buf); closesocket(sclient); wsacleanup(); server: #includewinsock2.h #includ

7、estdio.h #includestdlib.h #pragma comment(lib,ws2_32.lib) /服务器使用的端口号为 5050 #define default_port 5050 void main() int iport=default_port; wsadata wsadata; socket slisten, saccept; /客户端地址长度 int ilen; /发送的数据长度 int isend; /要发送给客户端的信息 char buf=i am a server.; /服务器和客户端的 ip 地址 struct sockaddr_in ser, cli;

8、printf(-n); printf(server waitingn); printf(-n); if(wsastartup(makeword(2,2),wsadata)!=0) printf(failed to load winsock.n); return; /创建服务器端套接口 slisten=socket(af_inet,sock_stream,0); if(slisten=invalid_socket) printf(socket() failed:%dn,wsagetlasterror(); return; /以下建立服务器端地址 ser.sin_family=af_inet; /

9、htons()函数把一个双字节的主机直接顺序的数据转换为网络直接顺序的数 ser.sin_port=htons(iport); /htonl()函数把一个四字节的主机直接顺序的数据转换为网络直接顺序的数 /使用系统制定的 ip 地址 inaddr_any ser.sin_addr.s_addr=htonl(inaddr_any); if(bind(slisten,(lpsockaddr)ser,sizeof(ser)=socket_error) printf(bind() failed: %dn,wsagetlasterror(); return; /进入监听状态 if(listen(slis

10、ten,5)=socket_error) printf(listen() failed:%dn,wsagetlasterror(); return; /初始化客户端地址长度参数 ilen=sizeof(cli); /进入一个无限循环,等待客户的连接请求 while(1) saccept=accept(slisten,(struct sockaddr*)cli,ilen); if(saccept=invalid_socket) printf(accept() failed: %dn,wsagetlasterror(); break; /输出客户 ip 地址和端口号 printf(accepted

11、 client ip:%s,port:%dn,inet_ntoa(cli.sin_addr),ntohs(cli.sin_port); /给建立连接的客户发送信息 isend=send(saccept,buf,sizeof(buf),0); if(isend=socket_error) printf(send() failed: %dn,wsagetlasterror(); break; else if(isend=0) break; else printf(send() byte:%dn,isend); printf(-n); closesocket(saccept); closesocke

12、t(slisten); wsacleanup(); 实验截图: 实验二 基于 tcp 协议的客户/服务器通信程序 实验目的: 通过实验,使学生熟悉并掌握运用 tcp/ip 技术进行网络编程的基本知识,加深对课堂教学内容的理解,掌握套接字网络通信编程技术,能够运用 vc+为开发工具编程解决网络通信中的实际问题,进行一些简单的网络应用程序设计。 实验内容: 1,主机间 tcp 的性能测试之一:回程时延。 2,服务器端能从客户端接收数据并立即将接收到的数据原样返回给客户方。 3,客户端能往服务器端发送数据,然后立即接受从服务器端原样返回的数据。 理解 tcp 通信程序设计过程,并结合特定应用场景(如

13、创建留言板程序、创建多客户端服务器/客户通信程序等)完成程序开发。掌握 tcp 服务器程序和客户程序的创建过程,熟悉单播通信程序中用到的 winsock 接口,培养学生将所学知识进行灵活运用的能力。 实验步骤: 1,打开 vc 环境 2,使用向导为客户端创建工程:选择可执行程序,选择使用 wsa 环境,单文档环境,其他的选择默认设置 3,在文件中添加代码 4,编译调试 5,使用向导为服务器端创建工程:选择可执行程序,选择使用 wsa 环境,单文档环境,其他的选择默认设置 6,在文件中添加代码 7,编译调试 8,分别打开两个系统命令窗口中,并分别在其中运行客户端和服务器端程序。 9,在客户端着输

14、入字符,可以看到服务器收到字符串 注:可以再实验一的代码上修改,自己增加额外的功能,比如取系统时间,计算往返时间等 作完之后,修改通信代码使用 udp 来实现网络通信 实验结果: client: #includewinsock2.h #includestdio.h #includestdlib.h #define default_port 5050 #define data_buffer 1024 #pragma comment(lib,ws2_32.lib) void main(int argc,char* argv) wsadata wsadata; socket sclient; int

15、 iport=5050; int ilen; int isend,irecv; char send_buf=hello! i am a client; char recv_bufdata_buffer; struct sockaddr_in ser; if(argc2) printf(输入服务器的 ip 地址:n); return; else memset(recv_buf,0,sizeof(recv_buf); if(wsastartup(makeword(2,2),wsadata)!=0) printf(winsock 环境初始化失败:n); return; sclient=socket(

16、af_inet,sock_dgram,0); if(sclient=invalid_socket) printf(socket()函数调用失败:%dn,wsagetlasterror(); return; ser.sin_family=af_inet; ser.sin_port=htons(iport); ser.sin_addr.s_addr=inet_addr(argv1); ilen=sizeof(ser); isend=sendto(sclient,send_buf,sizeof(send_buf),0,(struct sockaddr*)ser,ilen); if(isend=soc

17、ket_error) printf(sendto()函数调用失败:%dn,wsagetlasterror(); return; else if(isend=0) return; else printf(sendto()调用成功:n); irecv=recvfrom(sclient,recv_buf,sizeof(recv_buf),0,(struct sockaddr*)ser,ilen); if(irecv=socket_error) printf(recvfrom()函数调用失败:%dn,wsagetlasterror(); return; else if(irecv=0) return;

18、 else printf(sendto():%sn,recv_buf); printf(-n); closesocket(sclient); wsacleanup(); server: #includewinsock2.h #includestdio.h #includestdlib.h #define default_port 5050 #define buffer_length 1024 #pragma comment(lib,ws2_32.lib) void main() int iport=default_port; wsadata wsadata; socket ssocket; i

19、nt ilen,irecv,isend; struct sockaddr_in ser,cli; char send_buf=hollo!i am a server; char recv_bufbuffer_length; printf(-); printf(server waiting); printf(-); if(wsastartup(makeword(2,2),wsadata)!=0) printf(环境初始化错误:n); return; ssocket=socket(af_inet,sock_dgram,0); if(ssocket=invalid_socket) printf(so

20、cket()函数调用失败:n,wsagetlasterror(); return; ser.sin_family=af_inet; ser.sin_port=htons(iport); ser.sin_addr.s_addr=htonl(inaddr_any); if(bind(ssocket,(lpsockaddr)ser,sizeof(ser)=socket_error) printf(bind()函数调用失败:n,wsagetlasterror(); return; ilen=sizeof(cli); memset(recv_buf,0,sizeof(recv_buf); while(1

21、) irecv=recvfrom(ssocket,recv_buf,buffer_length,0,(sockaddr*)cli,ilen); if(irecv=socket_error) printf(recvfrom()函数调用失败:n,wsagetlasterror(); break; else if(irecv=0) break; else printf(recvfrom():%dn,recv_buf); printf( 客 户 端 的 ip 地 址 、 端 口号:%dn,inet_ntoa(cli.sin_addr),ntohs(cli.sin_port); isend=sendto

22、(ssocket,send_buf,sizeof(send_buf),0,(sockaddr*)cli,sizeof(cli); if(isend=socket_error) printf(sendto()函数调用失败:n,wsagetlasterror(); break; else if(isend=0) break; else printf(sendto():调用成功!n); closesocket(ssocket); wsacleanup(); 实验截图: 实验三 简易聊天系统的实现 实验目的: 通过实验,使学生熟悉并掌握运用 tcp/ip 技术进行网络编程的基本知识,加深对课堂教学内容

23、的理解,掌握套接字网络通信编程技术,能够运用 vc+为开发工具编程解决网络通信中的实际问题,进行一些简单的网络应用程序设计。 实验内容: 设计实现包括客户端和服务器端的简单聊天系统。 通过编写简单的聊天程序,理解 mfc 的 socket 类同 socket api 之间的区别以及mfc 的两种类之间的联系与区别。 实验步骤: 1,打开 vc 环境 2,使用向导为客户端创建工程:选择 fmc 可执行程序,选择使用 wsa 环境,选择单文档环境,其他的选择默认设置 3,为对话窗添加控件:右击工具栏选择控件,拖拽某个控件到对话框 4,为控件添加变量:使用类向导,选择要操作的对话窗类,选择变量 ta

24、b,点击添加变量按钮,为变量命名并选择变量类型。 5,为控件添加代码:右击控件添加事件,如点击,双击,右击。为事件添加代码,根据教科书添加代码 6,添加新的对话窗:单机 rousource tab, 在对话窗出右击,选择添加对话窗, 7,为对话窗添加类:右键点击新对话窗,选择添加类,出现向导,为类命名并选择父类 8,为心对话窗添加控件和变量 9,为新对话窗添加代码 10, 编译调试 参考代码:课本 224 页-229 页 实验结果: csockclient: #include stdafx.h #include csockclient.h #include csockclientdlg.h #

25、ifdef _debug #define new debug_new #undef this_file static char this_file = _file_; #endif / / ccsockclientapp begin_message_map(ccsockclientapp, cwinapp) /afx_msg_map(ccsockclientapp) / note - the classwizard will add and remove mapping macros here. / do not edit what you see in these blocks of gen

26、erated code! /afx_msg on_command(id_help, cwinapp:onhelp) end_message_map() / / ccsockclientapp construction ccsockclientapp:ccsockclientapp() / todo: add construction code here, / place all significant initialization in initinstance / / the one and only ccsockclientapp object ccsockclientapp theapp

27、; / / ccsockclientapp initialization bool ccsockclientapp:initinstance() if (!afxsocketinit() afxmessagebox(idp_sockets_init_failed); return false; afxenablecontrolcontainer(); / standard initialization / if you are not using these features and wish to reduce the size / of your final executable, you

28、 should remove from the following / the specific initialization routines you do not need. #ifdef _afxdll enable3dcontrols(); / call this when using mfc in a shared dll #else enable3dcontrolsstatic(); / call this when linking to mfc statically #endif ccsockclientdlg dlg; m_pmainwnd = dlg; int nrespon

29、se = dlg.domodal(); if (nresponse = idok) / todo: place code here to handle when the dialog is / dismissed with ok else if (nresponse = idcancel) / todo: place code here to handle when the dialog is / dismissed with cancel / since the dialog has been closed, return false so that we exit the / applic

30、ation, rather than start the application"s message pump. return false; csockserver: #include stdafx.h #include csockserver.h #include csockserverdlg.h #ifdef _debug #define new debug_new #undef this_file static char this_file = _file_; #endif / / ccsockserverapp begin_message_map(ccsockserverap

31、p, cwinapp) /afx_msg_map(ccsockserverapp) / note - the classwizard will add and remove mapping macros here. / do not edit what you see in these blocks of generated code! /afx_msg on_command(id_help, cwinapp:onhelp) end_message_map() / / ccsockserverapp construction ccsockserverapp:ccsockserverapp()

32、/ todo: add construction code here, / place all significant initialization in initinstance / / the one and only ccsockserverapp object ccsockserverapp theapp; / / ccsockserverapp initialization bool ccsockserverapp:initinstance() if (!afxsocketinit() afxmessagebox(idp_sockets_init_failed); return fa

33、lse; afxenablecontrolcontainer(); / standard initialization / if you are not using these features and wish to reduce the size / of your final executable, you should remove from the following / the specific initialization routines you do not need. #ifdef _afxdll enable3dcontrols(); / call this when u

34、sing mfc in a shared dll #else enable3dcontrolsstatic(); / call this when linking to mfc statically #endif ccsockserverdlg dlg; m_pmainwnd = dlg; int nresponse = dlg.domodal(); if (nresponse = idok) / todo: place code here to handle when the dialog is / dismissed with ok else if (nresponse = idcance

35、l) / todo: place code here to handle when the dialog is / dismissed with cancel / since the dialog has been closed, return false so that we exit the / application, rather than start the application"s message pump. return false; 实验截图: 实验四 wininet 实现 ftp 客户端 实验目的: 通过实验,使学生熟悉并掌握运用 tcp/ip 技术进行网络编程的

36、基本知识,加深对课堂教学内容的理解,掌握套接字网络通信编程技术,能够运用 vc+为开发工具编程解决网络通信中的实际问题,进行一些简单的网络应用程序设计。 实验内容: 1,写出完整的软件需求说明书。 2,开发 ftp 的客户端。 3,完成在局域网内的测试,并记录测试结果。 本实验涵盖了 c/s 体系结构和 socket 编程。通过本实验深入地了解 ftp 的工作原理以及服务器端和客户端的工作流程,学习 socket 在网络编程中的各种应用,掌握 wininet 的套接字编程。 实验步骤: 1,打开 vc 环境 2,使用向导为客户端创建工程:选择 mfc 可执行程序,单文档环境,其他的选择默认设置

37、 3,为对话窗添加控件:右击工具栏选择控件,拖拽某个控件到对话框 4,为控件添加变量:使用类向导,选择要操作的对话窗类,选择变量 tab,点击添加变量按钮,为变量命名并选择变量类型。 5,为控件添加代码:右击控件添加事件,如点击,双击,右击。为事件添加代码,根据教科书添加代码 6,编译调试 实验结果: scan: #include stdafx.h #include scan.h #include scandlg.h #ifdef _debug #define new debug_new #undef this_file static char this_file = _file_; #endif / / cscanapp begin_message_map(cscanapp, cwinapp) /afx_msg_map(cscan

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论