北邮国际学院互联网应用cousework.docx
- 文档编号:30463798
- 上传时间:2023-08-15
- 格式:DOCX
- 页数:18
- 大小:766.55KB
北邮国际学院互联网应用cousework.docx
《北邮国际学院互联网应用cousework.docx》由会员分享,可在线阅读,更多相关《北邮国际学院互联网应用cousework.docx(18页珍藏版)》请在冰豆网上搜索。
北邮国际学院互联网应用cousework
Pop3
Thepop3client-sideprogramisintendtofulfilthefollowingthreemaintasks:
1.Resolvethepop3serverdomainnameandconvertitintoIPaddresstoestablishaTCPconnectionbetweentheclientandtheserver.
2.GuidetheuserlogintheE-mail.
3.ProvidecommonfunctionsofE-mailfortheuser,includinggetthenumberandlistoftheE-mails,displaythecontentofmails,deletethemails,searchtextinmailsanddisplaythemailsbysubject.
Specificallyspeaking,thelog-inprocess'srequirements:
Afterestablishingtheconnectionwiththeserversuccessfully,requireusernameandpasswordfromtheuserandcheckwhethertheyarevalid.
Thefivecommonfunctions'requirementsare:
1.GetthenumberandlistoftheE-mails
Whenuserentersthecorrespondingcommand,theprogramcandisplaythenumberorthelistoftheE-mailsorderlyonscreen.
2.Displaythecontentofmails
Allowuserstochoosethemailtheywanttocheckbyprovidingtwochoices---downloadandprintitonscreen.
3.Deletethemails
Allowuserstochoosethemailtheywanttodeleteanddeleteitfromthemailbox.
4.searchtextinmails
Whenuserentersthetextstheywanttosearch,theprogramwilldisplaythefilenamesofthefilescontainit.
5.Displaythemailsbysubject
DisplaytheE-mailbysubject,meaningfileswiththesamesubjectswillbegroupedtogether.
Therefore,Idecomposethewholeprogramintothreepartsbywritingtwootherfunctionsapartfrommain()--voidlogOn()andvoidlogged().Thefirsttaskisimplementedinmain()bycallingthefunctionintgetIP();thesecondtaskisimplementedinthefunctionvoidlogged();thelastoneisimplementedinthefunctionvoidlogOn().SincetherearetoomanyfunctionsneedtobeachievedinthelogOn(),fiveotherfunctionsarecalledinit,theyare:
intprintMail();intchkEnd(int);intmakeChoice(char*);voidsrch();voidsortSub().
Thus,thewholeprogramisdecomposedinto10modules(10functions)asshowninthebelowchart:
intmain():
themainfunctioniswhereaprogramstartsexecution;itcallsthreefunctionsinit:
intgetIP(),voidlogOn()andvoidlogged().
intgetIP(char[]):
itisusedtoconvertthereceivedpop3serverdomainnametoIPaddress.Whencalledinmain()function,anargumentinchar[]typewillbepassedtoitandthenconvertedtoIPaddressindotteddecimalnotation.
voidlogOn():
itiscalledinfunctionmain()andusedforguidingtheuserlogontoE-mail.Thefunctionintmygetch()iscalledinittogeteachenteredcharacterimplicitly.
voidlogged():
itiscalledinfunctionmain()toprovide7commonfunctionsofE-mailstotheuser.
intmygetch():
itiscalledinfunctionvoidlogOn()tomaketheenteredcharacterfromtheuserinvisible.
intprintMail():
itiscalledinfunctionvoidlogged()toprintanddownloadtheE-mail.FunctionintchkEnd(int)iscalledbyittochecktheendofthemessage.
intchkEnd(int):
itisusedtocheckwetherthereceivedmessagefromtheserverhasreachedtotheend.WhencalledinthefunctionintprintMail(),thelengthofthereceivedininttypeispassedtoit.Itwillthenreturn0or1toindicatewhetheritistheendofthemail.
intmakeChoice(char*a):
itisusedtocheckwhichcommanddoestheuserinput.Whencalledinthefunctionlogged(),theuser'scommandinchar*typeispassedtoit.Then,itreturnthecorrespondingintegerininttype.
voidsrch():
itiscalledbythefunctionlogged()toaccomplishthe"searchbytext"task.
voidsortSub():
itiscalledbythefunctionlogged()toaccomplishthe"displaybysubject"task.
Overallflowchart
Designofdatastructure
Intheprogram,thereareelevenglobalvaribles:
FILE_MODE0644
----Definethemodeofopeningafile.
PORT110
----PortnumberfortheTCPconnection.
char*IP
----holdtheserver'sIPaddress.
msg[250]
----holdthereceived
comd[2][200]={"USER","PASS"},
command[5][200]={"STAT\n","LIST\n","RETR","DELE""QUIT\n"},
mail[250];
intrecvMsgSize,sock,qt=0,dl=0;
intmain():
First,Iuseawhileloop,inwhichtheIPresolveandlogontasksareimplemented.Aglobalvaribleipininttypeisusedtocontroltheloop.Itissetto0atthebeginning.WhenthefunctionintgetIP()successfullyconvertthereceivedpop3serverdomainnameintotheIPaddress,itwillreturn1tothevaribleip,theipwillbesetto1sothatthewhileloopcanbeended.Then,iftheexpressionstrstr(msg,"+OK")!
=NULListrue,thefunctionvoidlogOn()andvoidlogged()willbecalledconsequentlytofinishtheothertwotasks,ifitisfalse,thesocketwillbeclosedandtheprogramwillbeended.
intprintMail();
Itiscalledinfunctionvoidlogged()toprintanddownloadtheE-mail.Astheflowchartshows,theprogramfirstprovidetwochoicesfortheuser----1and2.Iftheuserenter1,afilenameisrequiredfromtheuserinordertocreateafileholdingthemail.Ifthechoiceis2,themailwillbeprintedoutonthescreen.Toreceivethemailproperly,awhileloopisusedwhichiscontrolledbythevariableiininttype.Itissettozeroatthebeginning.Then250charactersarereceivedintheglobalvariablecharmail[250]eachtime,ifthelengthofmailislessthan250,thefunctionintchkEnd()iscalledtocheckwetheritreachestheendofthemail.Ifitistheend,thefunctionwillreturn1,whichisgiventoitoendthewhileloop.
intmakeChoice(char*a);
Itisusedtocheckwhichcommanddoestheuserinput.Whencalledinthefunctionlogged(),theuser'scommandinchar*typeispassedtoit.Then,itreturnthecorrespondingintegerininttype.Astheflowchartshows,whentheuser'scommandis"stat","ls","find"or"sort",thevariableintchoicewillbesetto0,1,5and6orderly.Whenthecommandis"get"or"dele",theprogramwillrequiretheusertoenterthenumberofthemailtheywanttogetordelete,thensetchoiceto2and3correspondingly.Iftheenteredcommandis"quit",theglobalvariableqtissetto1andthechoiceissetto4.Ifthecommandisnotmatchtoallthecommandmentionedabove,thechoiceissetto7.Atlast,returnchoice.
voidlogOn()
Itiscalledinfunctionmain()andusedforguidingtheuserlogontotheE-mail.Thefunctionintmygetch()iscalledinittogeteachcharacterimplicitly.
Astheflowchartshows,awhileloopisusedtoallowtheuserentertheusernameandpasswordrepeatedly.Itfirstrequireausernamefromtheuser.Thengenerateandsendthepop3commandout.Ifthestrstr(msg,"+OK")!
=NULL,thenrequiretheuserenterthepassword.Tomakethepasswordenteredimplicitly,thefunctionintmygetch()iscalledinaforloop.Ifthereceivedcharacteris'\n',theforloopisendedifnotprint'*'.Then,sendthepasswordinpop3style,ifthestrstr(msg,"+OK")!
=NULL,printoutthereceivedmessageandstepoutthewhileloop.
voidlogged():
Itiscalledinfunctionmain()toprovide7commonfunctionsoftheE-mailtotheuser.Awhileloopisusedinittoallowtheuserrepeatingchoosethecommand.Theglobalvariableqtininttypeisusedtocontrolthewhileloop.Itissetto0atthebeginning.Intheloop,theprogramfirstprovidesomeinformationsabouthowtochoosedifferentcommandstorealizedifferentfunctions.Then,itcallsthefunctionmakeChoice()bypassingthestringgotfromuser'stoit,thereturnvalueofthefunctionisgiventoainttypevariablen.Accordingtothevalueofn,correspondingpop3commandissentoutandcorrespondingfunctionsarecalled.Thewhileloopwillcontinuetorununlesstheenteredcommandfromtheuseris"quit".BecausewhenthestringpassedinthefunctionintmakeChoice()is"quit",theglobalvariableqtwillbesetto1sothatthewhileloopcanbeended.
sortSub()
itiscalledbythefunctionlogged()toaccomplishthe"displaybysubject"task.Thisfunctioniswrittenontheconditionthatthefilenameareallnumberssothataforloopcanbeadoptedtoopeneachofthemtosorttheirsubjects.Anarrayofstrings--chars[7][100]isdefinedtoholdthesubjectofeachfileinordertosortthesubjectsinthesecondforloop.AvariablefsinFILE*typeisdefinedtosetupafilesteam.Inthefirstforloop,Ididin'tsetaconditiontocontroltheloopsothatitcancontinuerunninguntilafailureofopenningthefileaccurs.Eachtimeintheloop,Iusethefunctionsnprintftoconvertthevariablenininttypetoastringstoredinvariablejandopenthefilenamed"m".IfthefsisNULL,breaktostepoutofthelooptodothesecondforloop.Ifnot,enterawhileloop,inwhichIusefgetstoreadalineinthefileeachtime.Ifthefirstcharacterinthelineis'S',gettheremainingpartafterthestring''Subject"byusingthestrtokfunctionandcopyittos[i].Thenclosethefilesteamandbreakthewhileloop.Inthesecondforloop,Iusebubblesortmethodtosortthesubjectsandprintthemout.Tocomparethesubjects,thefunctionstrcmpisused.
voidsrch()
Thisfunctioniswrittenontheconditionthatthefilenameareallnumbers.First,theprogrampromptamessagetoasktheuserenterthetexttheywantsearchandsavethetextintothevariablecontentinchar[]type.Then,aforloopisadoptedtoopeneachfiletofindthetext.Intheloop,Ifirstusefunctionsnprintf(j,2,"%ld",i)toconvertthevariableiinunsignedlongtypetojinchar[]type.Thenopenthefilenamedj,ifthefilecannotopen,print"Done!
"andbreaktheloop,ifnot,readthefileintothevariablebufinchar[].Usethefunctionstrstrtofindthetextinthebuf.Iftheexpression--strstr(buf,content)==NULListrue,print"jisnone"wherejisthefilena
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 国际 学院 互联网 应用 cousework
![提示](https://static.bdocx.com/images/bang_tan.gif)