Tải bản đầy đủ - 0 (trang)
8 read, readv, recvfrom, and recvmsg System Calls

8 read, readv, recvfrom, and recvmsg System Calls

Tải bản đầy đủ - 0trang

InNet/3,recvisimplementedasa

libraryfunctionthatcallsrecvfrom.For

binarycompatibilitywithpreviously

compiledprograms,thekernelmaps

theoldrecvsystemcalltothefunction

orecv.Wediscussonlythekernel

implementationofrecvfrom.

Thereadandreadvsystemcallsarevalid

withanydescriptor,buttheremainingcalls

arevalidonlywithsocketdescriptors.

Aswiththewritecalls,multiplebuffersare

specifiedbyanarrayofiovecstructures.

Fordatagramprotocols,recvfromand

recvmsgreturnthesourceaddress

associatedwitheachincomingdatagram.

Forconnection-orientedprotocols,

getpeernamereturnstheaddress

associatedwiththeotherendofthe

connection.Theflagsassociatedwiththe

receivecallsareshowninSection16.11.

Aswiththewritecalls,thereceivecalls







utilizeacommonfunction,inthiscase

soreceive,todoallthework.Figure16.28

illustratestheflowofcontrolfortheread

systemcalls.



Figure16.28.Allsocketinputis

processedbysoreceive.



Wediscussonlythethreeshadedfunctions

inFigure16.28.Theremainingfunctions

areleftforreaderstoinvestigateontheir

own.



Team-Fly









Top



Team-Fly











TCP/IPIllustrated,Volume2:The

ImplementationByGaryR.Wright,

W.RichardStevens

TableofContents



Chapter16.SocketI/O



16.9recvmsgSystemCall

Therecvmsgfunctionisthemostgeneral

readsystemcall.Addresses,control

information,andreceiveflagsmaybe

discardedwithoutnotificationifaprocess

usesoneoftheotherreadsystemcalls

whilethisinformationispending.Figure

16.29showstherecvmsgfunction.



Figure16.29.recvmsgsystemcall.







433-445

Thethreeargumentstorecvmsgare:the

socketdescriptor;apointertoamsghdr

structure;andseveralcontrolflags.



Copyiovarray

446-461



Aswithsendmsg,recvmsgcopiesthe

msghdrstructureintothekernel,allocates

alargeriovecarrayiftheautomaticarray

aiovistoosmall,andcopiesthearray

entriesfromtheprocessintothekernel

arraypointedtobyiov(Section16.4).The

flagsprovidedasthethirdargumentare

copiedintothemsghdrstructure.



recvitandcleanup

462-470

Afterrecvithasreceiveddata,themsghdr

structureiscopiedbackintotheprocess

withtheupdatedbufferlengthsandflags.

Ifalargeriovecstructurewasallocated,it

isreleasedbeforerecvmsgreturns.



Team-Fly









Top



Team-Fly











TCP/IPIllustrated,Volume2:The

ImplementationByGaryR.Wright,

W.RichardStevens

TableofContents



Chapter16.SocketI/O



16.10recvitFunction

TherecvitfunctionshowninFigures16.30

and16.31iscalledfromrecv,recvfrom,

andrecvmsg.Itpreparesauiostructure

forprocessingbysoreceivebasedonthe

msghdrstructurepreparedbytherecvxxx

calls.



Figure16.30.recvitfunction:initializeuio

structure.



Figure16.31.recvitfunction:return

results.







471-500

getsockreturnsthefilestructureforthe

descriptors,andthenrecvitinitializesthe

uiostructuretodescribeareadtransfer

fromthekerneltotheprocess.The

numberofbytestotransferiscomputed



bysummingthemsg_iovlenmembersof

theiovecarray.Thetotalissavedin

uio_residandinlen.

Thesecondhalfofrecvit,showninFigure

16.31,callssoreceiveandcopiesthe

resultsbacktotheprocess.



Callsoreceive

501-510

soreceiveimplementsthecomplex

semanticsofreceivingdatafromthe

socketbuffers.Thenumberofbytes

transferredissavedin*retsizeand

returnedtotheprocess.Whenansignal

arrivesorablockingconditionoccursafter

somedatahasbeencopiedtotheprocess

(lenisnotequaltouio_resid),theerroris

discardedandthepartialtransferis

reported.



Copyaddressandcontrolinformationto

theprocess

511-542



Iftheprocessprovidedabufferforan

addressorcontrolinformationorboth,the

buffersarefilledandtheirlengthsadjusted

accordingtowhatsoreceivereturned.An

addressmaybetruncatedifthebufferis

toosmall.Thiscanbedetectedbythe

processifitsavesthebufferlengthbefore

thereadcallandcomparesitwiththe

valuereturnedbythekernelinthe

namelenpvariable(orinthelengthfieldof

thesockaddrstructure).Truncationof

controlinformationisreportedbysetting

MSG_CTRUNCinmsg_flags.Seealso

Exercise16.7.



Cleanup

543-549

Atout,thembufsallocatedforthesource

addressandthecontrolinformationare

released.



Team-Fly









Top



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

8 read, readv, recvfrom, and recvmsg System Calls

Tải bản đầy đủ ngay(0 tr)

×