您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > Network programming Nov 27, 2001
NetworkprogrammingNov27,2001Topics•Client-servermodel•Socketsinterface•Echoclientandserverclass26.ppt15-213“ThecoursethatgivesCMUitsZip!”CS213F’01–2–class26.pptClient-servermodelclientprocessserverprocess1.clientsendsrequest2.serverhandlesrequest3.serversendsresponse4.clienthandlesresponseresourceEverynetworkapplicationisbasedontheclient-servermodel:•Applicationisaserverprocessandoneormoreclientprocesses•Servermanagessomeresource,andprovidesservicebymanipulatingresourceforclients.•Clientmakesarequestforaservice–requestmayinvolveaconversationaccordingtosomeserverprotocol•ServerprovidesservicebymanipulatingtheresourceonbehalfofclientandthenreturningaresponseCS213F’01–3–class26.pptClientsExamplesofclientprograms•Webbrowsers,ftp,telnet,sshHowdoestheclientfindtheserver?•Theaddressoftheserverprocesshastwoparts:IPaddress:port–TheIPaddressisaunique32-bitpositiveintegerthatidentifiesthehost(adapter).»dotteddecimalform:0x8002C2F2=128.2.194.242–Theportispositiveintegerassociatedwithaservice(andthusaserverprocess)onthatmachine.»port7:echoserver»port23:telnetserver»port25:mailserver»port80:webserverCS213F’01–4–class26.pptUsingportstoidentifyservicesclientWebserver(port80)clientmachineservermachine128.2194.242kernelEchoserver(port7)servicerequestfor128.2.194.242:80(i.e.,theWebserver)clientWebserver(port80)kernelEchoserver(port7)servicerequestfor128.2.194.242:7(i.e.,theechoserver)CS213F’01–5–class26.pptServersServersarelong-runningprocesses(daemons).•Createdatboot-time(typically)bytheinitprocess(process1)•Runcontinuouslyuntilthemachineisturnedoff.Eachserverwaitsforrequeststoarriveonawell-knownportassociatedwithaparticularservice.•port7:echoserver•port25:mailserver•port80:httpserverAmachinethatrunsaserverprocessisalsooftenreferredtoasa“server”.CS213F’01–6–class26.pptServerexamplesWebserver(port80)•resource:files/computecycles(CGIprograms)•service:retrievesfilesandrunsCGIprogramsonbehalfoftheclientFTPserver(20,21)•resource:files•service:storesandretrievefilesTelnetserver(23)•resource:terminal•service:proxiesaterminalontheservermachineMailserver(25)•resource:email“spool”file•service:storesmailmessagesinspoolfileSee/etc/servicesforacomprehensivelistoftheservicesavailableonaLinuxmachine.CS213F’01–7–class26.pptThetwobasicwaysthatclientsandserverscommunicateConnections(TCP):•reliabletwo-waybyte-stream.•lookslikeafile.•akintoplacingaphonecall.•slowerbutmorerobust.Datagrams(UDP):•datatransferredinunreliablechunks.•canbelostorarriveoutoforder.•akintousingsurfacemail.•fasterbutlessrobust.Wewillonlydiscussconnections.clientserver...,Bk,Bk-1,...,B1,B0B0,B1,...,Bk-1,Bk,...connectionclientserverdgramdgramdgramdgramCS213F’01–8–class26.pptInternetconnections(review)Clientsandserverscommunicatebysendingstreamsofbytesoverconnections:•point-to-point,full-duplex,andreliable.Asocketisanendpointofaconnection•SocketaddressisanIPaddress:portpairAportisa16-bitintegerthatidentifiesaprocess:•ephemeralport:assignedautomaticallyonclientwhenclientmakesaconnectionrequest•well-knownport:associatedwithsomeserviceprovidedbyaserver(e.g.,port80isassociatedwithWebservers)Aconnectionisuniquelyidentifiedbythesocketaddressesofitsendpoints(socketpair)•(cliaddr:cliport,servaddr:servport)CS213F’01–9–class26.pptAnatomyofanInternetconnection(review)connectionsocketpair(128.2.194.242:51213,208.216.181.15:80)server(port80)clientclientsocketaddress128.2.194.242:51213serversocketaddress208.216.181.15:80clienthostaddress128.2.194.242serverhostaddress208.216.181.15CS213F’01–10–class26.pptBerkeleySocketsInterfaceCreatedintheearly80’saspartoftheoriginalBerkeleydistributionofUnixthatcontainedanearlyversionoftheInternetprotocols.Providesauser-levelinterfacetothenetwork.UnderlyingbasisforallInternetapplications.Basedonclient/serverprogrammingmodel.CS213F’01–11–class26.pptWhatisasocket?Asocketisadescriptorthatletsanapplicationread/writefrom/tothenetwork.•Keyidea:UnixusesthesameabstractionforbothfileI/OandnetworkI/O.Clientsandserverscommunicatewitheachbyreadingfromandwritingtosocketdescriptors.•UsingregularUnixreadandwriteI/Ofunctions.ThemaindifferencebetweenfileI/OandsocketI/Oishowtheapplication“opens”thesocketdescriptors.CS213F’01–12–class26.pptKeydatastructuresDefinedin/usr/include/netinet/in.hInternet-stylesocketsarecharacterizedbya32-bitIPaddressandaport./*Internetaddress*/structin_addr{unsignedints_addr;/*32-bitIPaddress*/};/*Internetstylesocketaddress*/structsockaddr_in{unsignedshortintsin_family;/*Addressfamily(AF_INET)*/unsignedshortintsin_port;/*Portnumber*/structin_addrsin_addr;/*IPaddress*/unsignedcharsin_zero[...];/*Padtosizeof“structsockaddr”*/};CS213F’01–13–class26.pptKeydatastructuresDefinedin/usr/include/netdb.hhostentisaDNShostentrythatassociatesadomainname(e.g.,cmu.edu)withanIPaddr(128.2.35.186)•Canbeaccessedfromuserprograms–gethostbyname()[domainnamekey]–gethostbyaddr()[IPaddresskey]•Canalsobeaccessedfromtheshellusingnslookupordig./*DomainNameService(DNS)hostentry*/structhostent{char*h_name;/*officialnameofhost*/char**h_aliases;/*aliaslist*/inth_addrtype;/*hos
本文标题:Network programming Nov 27, 2001
链接地址:https://www.777doc.com/doc-4963469 .html