TCP和UDP数据包发送与接收.docx
《TCP和UDP数据包发送与接收.docx》由会员分享,可在线阅读,更多相关《TCP和UDP数据包发送与接收.docx(24页珍藏版)》请在冰豆网上搜索。
![TCP和UDP数据包发送与接收.docx](https://file1.bdocx.com/fileroot1/2022-12/28/cb871cd2-da8a-4620-a0e9-7ac062d98a5a/cb871cd2-da8a-4620-a0e9-7ac062d98a5a1.gif)
TCP和UDP数据包发送与接收
TCP和UDP数据包发送与接收
实验6TCP和UDP数据包发送与接收一、实验目的
TCP协议是TCP/IP协议族的核心协议之一。
熟悉TCP包结构对于理解网络层次结构,以及TCP协议与IP协议的关系有着重要意义。
根据TCP协议的基本原理,通过封装与发送一个标准的TCP数据包,了解TCP包结构中各字段的含义与用途,从而深入理解传输层与下面各层的关系。
二、实验要求
(1)掌握TCP/UDP报头结构、各字段含义以及校验和计算方法;
(2)使用Wincap(Lipcap)构造并发送TCP,UDP数据包;
(3)使用原始套接字(RawSocket)发送自定义的TCP,UDP数据包;(4)使用NDIS协议驱动发送自定义的TCP/UDP数据包。
三、实验内容
实验一SOCKET编程实验
实验内容
1、通过调试、运行“UDPClient”和“UDPServer”实验程序,加强对网络通讯原理
的了解。
(或“简单Client”和“简单Server”实验程序,下同)
2、学习分析实验程序功能结构,了解基于SOCKET编程的网络通信软件的基本设计
方法。
3、在所提供的”UDPClient”和“UDPServer”实验程序基础上,完善程序功能。
4、通过实验学习和了解SOKCET通信的实现方法。
实验结果分析与总结
(1)总结运行”UDPClient”和“UDPServer”实验程序的运行情况。
UDPClient运行结
coefficientnotgreaterthan1.3;Highefficiency:
control1hourtraveltimebetweenanytwopointswithinthevariousgroupswithintheareauptohalfanhour,1-houraccesstotheairport,45minreachedthecity'smaintraffichub,externalhighwayforhalfanhourwiththemainchannelsconnectingbus1.5hoursbetweenanytwogroupsofcoreareas;Intensive:
2030transitsharerateofaround35%,2050bussharingrateof45%percent.3rdchapter,Yibincity,trafficsituationandGeneral...3.1.1citytrafficstatus1,externaltransportpassengerandcargotrafficinYibincity,trafficatthepresentmainrailway,highway,waterwayandairfourmodesofpassengerandfreighttransport,thereandculturalheritage,highlightingtheculturaltasteandshapecharacteristicsofthecityinYibin.Accordingtoforecasts,vision2050populationwillreach2.1millionpeopleinthecenterofYibincity,urbanlandisaround210km2.Figure2.6-12050figure2.7landuseplanningintheCenterCity,Yibincitydevelopmentrequiresthedevelopmentofbusrapidtransitconstructionandsocio-economicdevelopmenttrends,Yibincitycoordination,supportingurbanspacelayoutandinfrastructurenetworks,transportorganized,internalandexternaltraffichubsmoothlylinking,realizingsustainabledevelopmentofYibincityintegratedtransportationsystem.Concretecanbesummarizedas,accessible,efficient,intensiveandsustainable.Accessibility:
Centralbuilt-uparearoadnetworkdensityofabout7.0km/km2;buslinedensityof3-4km/km2,bustransfer
UDPServer运行结果
(2)设计交互程序的运行结果如下:
coefficientnotgreaterthan1.3;Highefficiency:
control1hourtraveltimebetweenanytwopointswithinthevariousgroupswithintheareauptohalfanhour,1-houraccesstotheairport,45minreachedthecity'smaintraffichub,externalhighwayforhalfanhourwiththemainchannelsconnectingbus1.5hoursbetweenanytwogroupsofcoreareas;Intensive:
2030transitsharerateofaround35%,2050bussharingrateof45%percent.3rdchapter,Yibincity,trafficsituationandGeneral...3.1.1citytrafficstatus1,externaltransportpassengerandcargotrafficinYibincity,trafficatthepresentmainrailway,highway,waterwayandairfourmodesofpassengerandfreighttransport,thereandculturalheritage,highlightingtheculturaltasteandshapecharacteristicsofthecityinYibin.Accordingtoforecasts,vision2050populationwillreach2.1millionpeopleinthecenterofYibincity,urbanlandisaround210km2.Figure2.6-12050figure2.7landuseplanningintheCenterCity,Yibincitydevelopmentrequiresthedevelopmentofbusrapidtransitconstructionandsocio-economicdevelopmenttrends,Yibincitycoordination,supportingurbanspacelayoutandinfrastructurenetworks,transportorganized,internalandexternaltraffichubsmoothlylinking,realizingsustainabledevelopmentofYibincityintegratedtransportationsystem.Concretecanbesummarizedas,accessible,efficient,intensiveandsustainable.Accessibility:
Centralbuilt-uparearoadnetworkdensityofabout7.0km/km2;buslinedensityof3-4km/km2,bustransfer
(3)总结程序设计的情况,列出所设计或修改部分的源代码清单。
附上程序源代码。
Client端修改的代码如下:
//(3)开始接收或发送过程
printf("\n-------------waitingformessagefromSeaver
-------------\n");
//进入一个循环
while
(1)
{
//输入并发送信息给服务器
buffer[0]='\0';//先清空发送缓冲区
printf("\nInputdatagramsendinfo(quit退出):
");//输入发送字符串
scanf("%s",buffer);
sendto(socketid,buffer,sizeofbuffer,0,(structsockaddr*)&server,server_len);
//发送信息
//控制循环退出
if(strcmp(buffer,"quit")==0)//输入为quit则结
束
{printf("\nsendinfoquit");
return0;
}
//接收服务器返回信息
buffer[0]='\0';//先清空接收缓冲区
if(recvfrom(socketid,buffer,sizeofbuffer,0,(structsockaddr*)&server,&server_len)!
=SOCKET_ERROR)//接收返回信息
{
coefficientnotgreaterthan1.3;Highefficiency:
control1hourtraveltimebetweenanytwopointswithinthevariousgroupswithintheareauptohalfanhour,1-houraccesstotheairport,45minreachedthecity'smaintraffichub,externalhighwayforhalfanhourwiththemainchannelsconnectingbus1.5hoursbetweenanytwogroupsofcoreareas;Intensive:
2030transitsharerateofaround35%,2050bussharingrateof45%percent.3rdchapter,Yibincity,trafficsituationandGeneral...3.1.1citytrafficstatus1,externaltransportpassengerandcargotrafficinYibincity,trafficatthepresentmainrailway,highway,waterwayandairfourmodesofpassengerandfreighttransport,thereandculturalheritage,highlightingtheculturaltasteandshapecharacteristicsofthecityinYibin.Accordingtoforecasts,vision2050populationwillreach2.1millionpeopleinthecenterofYibincity,urbanlandisaround210km2.Figure2.6-12050figure2.7landuseplanningintheCenterCity,Yibincitydevelopmentrequiresthedevelopmentofbusrapidtransitconstructionandsocio-economicdevelopmenttrends,Yibincitycoordination,supportingurbanspacelayoutandinfrastructurenetworks,transportorganized,internalandexternaltraffichubsmoothlylinking,realizingsustainabledevelopmentofYibincityintegratedtransportationsystem.Concretecanbesummarizedas,accessible,efficient,intensiveandsustainable.Accessibility:
Centralbuilt-uparearoadnetworkdensityofabout7.0km/km2;buslinedensityof3-4km/km2,bustransfer
printf("Receiveddatagramfrom--%s\n",buffer);
}
}
closesocket(socketid);//关闭SOCKET连
接
WSACleanup();//退出使用
wsock32.dll动态链接库
return0;
}
Seaver端修改的代码如下:
printf("\n-------------waitingformessagefromclient
-------------\n");
//进入一个循环
while
(1)
{
buffer[0]='\0';
if(recvfrom(socketid,buffer,sizeofbuffer,0,(structsockaddr*)&client,&client_len)!
=SOCKET_ERROR)
{
printf("Receiveddatagramfrom--%s\n",buffer);
//给cilent发信息
//charack[100]="recvok!
";
//sendto(socketid,ack,sizeofack,0,(structsockaddr*)&client,client_len);
}
buffer[0]='\0';
printf("\nInputdatagramsendinfo(quit退出):
");//输入发送字符串
scanf("%s",buffer);
sendto(socketid,buffer,sizeofbuffer,0,(structsockaddr*)&client,client_len);//发
if(strcmp(buffer,"quit")==0)//输入为
quit则结束
{
printf("\nsendinfoquit");
return0;
}
//Sleep(500);
}
closesocket(socketid);
WSACleanup();
return0;
}
总结:
coefficientnotgreaterthan1.3;Highefficiency:
control1hourtraveltimebetweenanytwopointswithinthevariousgroupswithintheareauptohalfanhour,1-houraccesstotheairport,45minreachedthecity'smaintraffichub,externalhighwayforhalfanhourwiththemainchannelsconnectingbus1.5hoursbetweenanytwogroupsofcoreareas;Intensive:
2030transitsharerateofaround35%,2050bussharingrateof45%percent.3rdchapter,Yibincity,trafficsituationandGeneral...3.1.1citytrafficstatus1,externaltransportpassengerandcargotrafficinYibincity,trafficatthepresentmainrailway,highway,waterwayandairfourmodesofpassengerandfreighttransport,thereandculturalheritage,highlightingtheculturaltasteandshapecharacteristicsofthecityinYibin.Accordingtoforecasts,vision2050populationwillreach2.1millionpeopleinthecenterofYibincity,urbanlandisaround210km2.Figure2.6-12050figure2.7landuseplanningintheCenterCity,Yibincitydevelopmentrequiresthedevelopmentofbusrapidtransitconstructionandsocio-economicdevelopmenttrends,Yibincitycoordination,supportingurbanspacelayoutandinfrastructurenetworks,transportorganized,internalandexternaltraffichubsmoothlylinking,realizingsustainabledevelopmentofYibincityintegratedtransportationsystem.Concretecanbesummarizedas,accessible,efficient,intensiveandsustainable.Accessibility:
Centralbuilt-uparearoadnetworkdensityofabout7.0km/km2;buslinedensityof3-4km/km2,bustransfer
在Client端接收返回信息发送信息和Seaver接收返回信息发送信息前都要进行清空接收缓冲区。
(1)掌握TCP/UDP报头结构、各字段含义以及校验和计算方法;
(a)TCP报头结构
TCP头部数据(data)
各字段含义:
源端口(SourcePort)和目的端口(DestinationPort):
分别代表本次TCP通信
发起主机和目的主机所使用的端口号;
序列号(SequenceNumber):
该字段用来标识TCP源端设备向目的端设备发
送的字节流,它表示在这个报文段中的第几个数据字节。
序列号是一个32
位的数。
确认号(AcknowledgeNumber):
TCP使用32位的确认号字段标识期望收到
的下一个段的第一个字节,并声明此前的所有数据已经正确无误地收到,因
此,确认号应该是上次已成功收到的数据字节序列号加1。
收到确认号的源
计算机会知道特定的段已经被收到。
确认号的字段只在ACK标志被设置时
才有效。
数据偏移(DataOffset):
这个4位字段包括TCP头大小。
由于首部可能含有
选项内容,因此TCP首部的长度是不确定的。
首部长度的单位是32比特或
4个八位组。
首部长度实际上也指示了数据区在报文段中的起始偏移值。
保留(Reserved):
6位置0的字段。
为将来定义新的用途保留。
、,控制位(ControlBits):
共6位,每一位标志可以打开一个控制功能。
URG(UrgentPointerFieldSignificant,紧急指针字段标志):
表示TCP包的
紧急指针字段有效,用来保证TCP连接不被中断,并且督促中间齐备尽快处
理这些数据。
ACK(Acknowledgementfieldsignificant,确认字段标志):
取1时表示应
答字段有效,也即TCP应答号将包含在TCP段中,为0则反之。
PSH(PushFunction,推功能):
这个标志表示Push操作。
所谓Push操作
就是指在数据包到达接收端以后,立即送给应用程序,而不是在缓冲区中排
队。
RST(Resettheconnection,重置连接):
这个标志表示感谢连接复位请求,
用来复位那些产生错误的连接,也被用来拒绝错误和非法的数据包。
SYN(Synchronizesequencenumbers,同步序列号):
表示同步序号,用来coefficientnotgreaterthan1.3;Highefficiency:
control1hourtraveltimebetweenanytwopointswithinthevariousgroupswithintheareauptohalfanhour,1-houraccesstotheairport,45minreachedthecity'smaintraffichub,externalhighwayforhalfanhourwiththemainchannelsconn