北邮国际学院互联网应用cousework.docx

上传人:b****8 文档编号:30463798 上传时间:2023-08-15 格式:DOCX 页数:18 大小:766.55KB
下载 相关 举报
北邮国际学院互联网应用cousework.docx_第1页
第1页 / 共18页
北邮国际学院互联网应用cousework.docx_第2页
第2页 / 共18页
北邮国际学院互联网应用cousework.docx_第3页
第3页 / 共18页
北邮国际学院互联网应用cousework.docx_第4页
第4页 / 共18页
北邮国际学院互联网应用cousework.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

北邮国际学院互联网应用cousework.docx

《北邮国际学院互联网应用cousework.docx》由会员分享,可在线阅读,更多相关《北邮国际学院互联网应用cousework.docx(18页珍藏版)》请在冰豆网上搜索。

北邮国际学院互联网应用cousework.docx

北邮国际学院互联网应用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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1