资源描述:
《Linux Socket Programming》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、SocketProgrammingWhatisasocket?UsingsocketsTypes(Protocols)AssociatedfunctionsStylesWewilllookatusingsocketsinCForJava,seeChapter2.6-2.8(optional)Note:Javasocketsareconceptuallyquitesimilar1Whatisasocket?AninterfacebetweenapplicationandnetworkTheapplicationcreatesaso
2、cketThesockettypedictatesthestyleofcommunicationreliablevs.besteffortconnection-orientedvs.connectionlessOnceconfiguredtheapplicationcanpassdatatothesocketfornetworktransmissionreceivedatafromthesocket(transmittedthroughthenetworkbysomeotherhost)2Twoessentialtypesofs
3、ocketsSOCK_STREAMa.k.a.TCPreliabledeliveryin-orderguaranteedconnection-orientedbidirectionalSOCK_DGRAMa.k.a.UDPunreliabledeliverynoorderguaranteesnonotionof“connection”–appindicatesdest.foreachpacketcansendorreceiveAppsocket321Dest.Appsocket321D1D3D2Q:whyhavetypeSOCK
4、_DGRAM?3SocketCreationinC:socketints=socket(domain,type,protocol);s:socketdescriptor,aninteger(likeafile-handle)domain:integer,communicationdomaine.g.,PF_INET(IPv4protocol)–typicallyusedtype:communicationtypeSOCK_STREAM:reliable,2-way,connection-basedserviceSOCK_DGRA
5、M:unreliable,connectionless,othervalues:needrootpermission,rarelyused,orobsoleteprotocol:specifiesprotocol(seefile/etc/protocolsforalistofoptions)-usuallysetto0NOTE:socketcalldoesnotspecifywheredatawillbecomingfrom,norwhereitwillbegoingto–itjustcreatestheinterface!4A
6、Socket-eyeviewoftheInternetEachhostmachinehasanIPaddressWhenapacketarrivesatahostmedellin.cs.columbia.edu(128.59.21.14)cluster.cs.columbia.edu(128.59.21.14,128.59.16.7,128.59.16.5,128.59.16.4)newworld.cs.umass.edu(128.119.245.93)5PortsPort0Port1Port65535Eachhosthas65
7、,536portsSomeportsarereservedforspecificapps20,21:FTP23:Telnet80:HTTPseeRFC1700(about2000portsarereserved)Asocketprovidesaninterfacetosenddatato/fromthenetworkthroughaport6Addresses,PortsandSocketsLikeapartmentsandmailboxesYouaretheapplicationYourapartmentbuildingadd
8、ressistheaddressYourmailboxistheportThepost-officeisthenetworkThesocketisthekeythatgivesyouaccesstotherightmailbox(onedifference:as