您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > TCP-IP程序设计_II
TCP/IP程序设计汪浩软件学院QQ:641461085引言Internet的应用程序WebServer/WebClientFTPServer/FTPClientMailServer/MailClientDNSServer/DNSClientQQ/eMule/xunlei/MSN/……Sniffer/网络安全工具编写这些Internet应用程序的基础是什么?TCP/IP协议TCP/IP(Socket)程序设计课程目标/内容熟练掌握Windows下的socket编程技术多进程模式TCPSocket程序设计多线程模式TCPSocket程序设计基于select复用模式的TCPSocket程序设计基于WSAAsyncSelect模式的TCPSocket程序设计基于WSAEventSelect模式的TCPSocket程序设计基于Overlapped模式的TCPSocket程序设计基于CompletionPort模式的TCPSocket程序设计WindowsTCPSocket的一些扩充函数以上各I/O模型的UDPSocket程序设计以上各I/O模型的RawSocket程序设计socket选项域名解析C#Socket程序设计OpenSSL的socket程序设计基于WinPcap的数据链路层程序设计教材及参考书TCP/IP协议W.RichardStevensetal.TCP/IPIllustrated,vol1~3(英文).清华大学出版社Socket编程W.RichardStevens.UnixNetworkProgramming(英文),2nd.清华大学出版社;3rd机械工业出版社AnthonyJones,JimOhlund.NetworkProgrammingforMsWindows.2nd.MicrosoftCorporationUnix编程W.RichardStevens,etal.AdvancedProgrammingintheUNIXEnviroment(英文).人民邮电出版社TCP/IP协议族体系结构概述TCP/IP与OSI的对应关系应用层应用层(HTTP/SMTP/POP3/FTP/DNS/SNMP…...)表示层会话层传输层传输层(TCP/UDP)网络层网络层--路由与分片(IP)(ARP/RARP/ICMP/IGMP)数据链路层网络接口层(Ethenet/ATM/FDDI)物理层OSITCP/IPInternet的基础协议是TCP/IP应用协议/程序HTTP—WebServer/WebClientSMTP/POP3—ExchangeServer/Foxmail/OutlookQQ/eMule/迅雷网游、即时通信……传输层(TCP/UDP)提供不同主机进程间的逻辑通信(SocketPairs)Microsoft,SUM,IBM网络层--路由与分片(IP)提供不同主机间的逻辑通信(IPAddr.)Cisco网络接口层(Ethenet/ATM/FDDI)提供连接在同一条链路上的设备间的逻辑通信3COM,D-Linketc编写Internet应用程序—SocketProgrammingGoal:learnhowtobuildclient/serverapplicationthatcommunicateusingsockets语言工具:C++C#JavaWEB应用程序应用程序QQ、eMule、网游应用程序ping应用程序Sniffer应用层WEBServer/WEBClient传输层TCPSocketUDPSocketTCPSocketUDPSocket网络层IPRawSocketIPRawSocketIPRawSocket网络接口层网络接口层网络接口层网络接口层网络接口层applicationtransportnetworkdatalinkphysicalapplicationtransportnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalnetworkdatalinkphysicalapplicationtransportnetworkdatalinkphysicalapplicationtransportnetworkdatalinkphysical传输层:提供不同主机进程间的逻辑通信套接口Socketpairs(源IP地址,源端口号;目标IP地址,目标端口号)网络层:提供不同主机间的逻辑通信IP地址(源IP地址;目标IP地址)“link”链路层:提供连接在同一条链路上的设备间的逻辑通信ProcessescommunicatingProcess:programrunningwithinahost.withinsamehost,twoprocessescommunicateusinginter-processcommunication(definedbyOS).processesindifferenthostscommunicatebyexchangingmessages(Sockets)Clientprocess:processthatinitiatescommunicationServerprocess:processthatwaitstobecontactedNote:applicationswithP2Parchitectureshaveclientprocesses&serverprocessesInter-processcommunicationofOS(e.g.Unix/Windows)UnixmessagepassingPipesandFIFOsMessagequeuesSynchronizationMutexesandconditionvariablesread-writelockssemaphoresSharedmemoryRemoteprocedurecallsWindowsWaitFunctions(WaitForSingleObjectetc)EventKernelObjectsWaitableTimerKernelObjectsSemaphoreKernelObjectsMutexKernelObjects数据的逐层封装应用层数据应用层数据应用层数据应用层数据TCP/UDP头IP头TCP/UDP头TCP/UDP头IP头物理帧头应用层传输层网络层网络接口层Client/Server编程模型Internet上的所有程序都是C/SC/Svs.B/SASimpleDaytimeClient/Server(IPv4)tcpdaytimecli.cpp/tcpdaytimesrv.cppClientIncludeourownheaderCreateTCPsocketSpecifyserver'sIPaddressandportEstablishconnectionwithserverReadanddisplayserver'sreplyTerminateprogramSocketPairThesocketpairforaTCPconnectionisthefour-tuplethelocalIPaddress,localport,foreignIPaddress,foreignportSocket(TCP/UDP)anIPaddress,aportnumbersourceport#destport#32bitsapplicationdata(variablelength)sequencenumberacknowledgementnumberReceivewindowUrgdatapnterchecksumFSRPAUheadlennotusedOptions(variablelength)Cont.ServerCreateaTCPsocketBindserver'swell-knownporttosocketConvertsockettolisteningsocketAcceptclientconnection,sendreplyTerminateconnectionErrorHandling:WrapperFunctionssockfd=Socket(AF_INET,SOCK_STREAM,0);Beginswithanuppercaseletterlib/wrapsock.cSomeElementSocketFunctionsandDataStructureslonga=0x12345678;0x120x340x560x780x780x560x340x12big-endianlittle-endian低地址高地址ByteOrderingFunctionsTheInternetprotocolsusebig-endianbyteorderingforthesemultibyteintegers--networkbyteorderlow-orderbytehigh-orderbyteAA+1high-orderbytelow-orderbyteAA+1Fourfunctionstoconvertbetweenthesetwobyteordershstandsforhost,nstandsfornetwork,sstandsforshort,lstandsforlong.SomeUsefulFunctionsSocketAddressStructures(1)IPv4SocketAddressStructure#includewinsock2.hIPaddr:192.168.1.1110000001010100000000001000000013232235777unsignedlong提供不同主机间的逻辑通信提供不同主机进程间的逻辑通信套接口Socketpairs/**Basicsystemtypedefinitions,takenfromtheBSDfilesys/types.h.*/typedefunsignedcharu_char;typedefunsignedshortu_short;typedefunsignedintu_int;typedefunsignedlongu_long;/**Thenewtypetobeusedinall*instanceswhichrefertosockets.*/typedefu_intSOCKET;structin_addr{union{struct{u_chars_b1,s_b2,s_b3,s_b4;}S_un_b;struct{u_shorts_w1,s_w2;}S_un_w;u_longS_addr;}S_un;#defines_addrS_un.S_addr/*canbeusedformosttcp&ipcode*/#defines_hostS_un.S_un_b.s_b2/*hostonimp*/#defines_netS_un.S_un_b.s_b1/*network*/#defines_impS_un.S_un_w.s_w2/*imp*/#defines_impnoS_un.S_un_b.s_b4/*imp#*/#defines_l
本文标题:TCP-IP程序设计_II
链接地址:https://www.777doc.com/doc-3510618 .html