首页 实验七应用层网络编程一

实验七应用层网络编程一

举报
开通vip

实验七应用层网络编程一TPMKstandardizationoffice【TPMK5AB-TPMK08-TPMK2C-TPMK18】实验七应用层网络编程一浙江大学城市学院实验报告课程名称计算机网络应用实验项目名称实验七应用层网络编程(一)实验成绩指导老师(签名)日期2014-06-03一.实验目的和要求通过实现使用Java应用层客户端和服务器来获得关于使用JavaSocket网络编程的经验(SMTP、POP3)。二.实验内容、原理及实验结果与分析SMTP编程(参考电子讲义“网络编程参考资料-应用层.pdf”及教材“第2章Socket编程...

实验七应用层网络编程一
TPMKstandardizationoffice【TPMK5AB-TPMK08-TPMK2C-TPMK18】实验七应用层网络编程一浙江大学城市学院实验 报告 软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载 课程名称计算机网络应用实验项目名称实验七应用层网络编程(一)实验成绩指导老师(签名)日期2014-06-03一.实验目的和要求通过实现使用Java应用层客户端和服务器来获得关于使用JavaSocket网络编程的经验(SMTP、POP3)。二.实验内容、原理及实验结果与分析SMTP编程(参考电子讲义“网络编程参考资料-应用层.pdf”及教材“第2章Socket编程”)阅读“网络编程参考资料-应用层.pdf”中8.3.1部分,实现“SMTP客户机实现”的源代码(SMTPClientDemo.java),并在机器上编译运行通过。(注:可输入城院SMTP邮件服务器smtp.email.zucc.edu.cn或其他邮件服务器作为SMTP服务器)【程序源代码】SMTPClientDemo.javaimportjava.io.*;importjava.net.*;importjava.util.*;//Chapter8,Listing1publicclassSMTPClientDemo{protectedintport=25;protectedStringhostname="localhost";protectedStringfrom="";protectedStringto="";protectedStringsubject="";protectedStringbody="";protectedSocketsocket;protectedBufferedReaderbr;protectedPrintWriterpw;//ConstructsanewinstanceoftheSMTPClientpublicSMTPClientDemo()throwsException{try{getInput();sendEmail();}catch(Exceptione){System.out.println("Errorsendingmessage-"+e);}}publicstaticvoidmain(String[]args)throwsException{//StarttheSMTPclient,soitcansendmessagesSMTPClientDemoclient=newSMTPClientDemo();}//ChecktheSMTPresponsecodeforanerrormessageprotectedintreadResponseCode()throwsException{Stringline=br.readLine();System.out.println("<"+line);line=line.substring(0,line.indexOf(""));returnInteger.parseInt(line);}//WriteaprotocolmessagebothtothenetworksocketandtothescreenprotectedvoidwriteMsg(Stringmsg)throwsException{pw.println(msg);pw.flush();System.out.println(">"+msg);}//Closeallreaders,streamsandsocketsprotectedvoidcloseConnection()throwsException{pw.flush();pw.close();br.close();socket.close();}//SendtheQUITprotocolmessage,andterminateconnectionprotectedvoidsendQuit()throwsException{System.out.println("SendingQUIT");writeMsg("QUIT");readResponseCode();System.out.println("ClosingConnection");closeConnection();}//SendanemailmessageviaSMTP,adheringtotheprotocolknownasRFC2821protectedvoidsendEmail()throwsException{System.out.println("Sendingmessagenow:Debugbelow");System.out.println("---------------------------------"+"-----------------------------");System.out.println("OpeningSocket");socket=newSocket(this.hostname,this.port);System.out.println("CreatingReader&Writer");br=newBufferedReader(newInputStreamReader(socket.getInputStream()));pw=newPrintWriter(newOutputStreamWriter(socket.getOutputStream()));System.out.println("Readingfirstline");intcode=readResponseCode();if(code!=220){socket.close();thrownewException("InvalidSMTPServer");}System.out.println("Sendinghelocommand");writeMsg("HELO"+InetAddress.getLocalHost().getHostName());code=readResponseCode();if(code!=250){sendQuit();thrownewException("InvalidSMTPServer");}System.out.println("Sendingmailfromcommand");writeMsg("MAILFROM:<"+this.from+">");code=readResponseCode();if(code!=250){sendQuit();thrownewException("Invalidfromaddress");}System.out.println("Sendingrcpttocommand");writeMsg("RCPTTO:<"+this.to+">");code=readResponseCode();if(code!=250){sendQuit();thrownewException("Invalidtoaddress");}System.out.println("Sendingdatacommand");writeMsg("DATA");code=readResponseCode();if(code!=354){sendQuit();thrownewException("Dataentrynotaccepted");}System.out.println("Sendingmessage");writeMsg("Subject:"+this.subject);writeMsg("To:"+this.to);writeMsg("From:"+this.from);writeMsg("");writeMsg(body);code=readResponseCode();sendQuit();if(code!=250)thrownewException("Messagemaynothavebeensentcorrectly");elseSystem.out.println("Messagesent");}//ObtaininputfromtheuserprotectedvoidgetInput()throwsException{//ReadinputfromuserconsoleStringdata=null;BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));//RequesthostnameforSMTPserverSystem.out.print("PleaseenterSMTPserverhostname:");data=br.readLine();if(data==null||data.equals(""))hostname="localhost";elsehostname=data;//Requestthesender'semailaddressSystem.out.print("PleaseenterFROMemailaddress:");data=br.readLine();from=data;//Requesttherecipient'semailaddressSystem.out.print("PleaseenterTOemailaddress:");data=br.readLine();if(!(data==null||data.equals("")))to=data;System.out.print("Pleaseentersubject:");data=br.readLine();subject=data;System.out.println("Pleaseenterplain-textmessage('.'character"+"onablanklinesignalsendofmessage):");StringBufferbuffer=newStringBuffer();//Readuntiluserentersa.onablanklineStringline=br.readLine();while(line!=null){//Checkfora'.',andonlya'.',onalineif(line.equalsIgnoreCase(".")){break;}buffer.append(line);buffer.append("\n");line=br.readLine();}buffer.append(".\n");body=buffer.toString();}}【实验结果与分析】POP3编程(参考电子讲义“网络编程参考资料-应用层.pdf”及教材“第2章Socket编程”)阅读“网络编程参考资料-应用层.pdf”中8.3.2部分,实现“POP3客户实现”的源代码(Pop3ClientDemo.java),并在机器上编译运行通过。(注:可输入城院POP3邮件服务器pop3.email.zucc.edu.cn或其他邮件服务器作为POP3服务器)【程序源代码】Pop3ClientDemo.javaimportjava.io.*;importjava.net.*;importjava.util.*;publicclassPop3ClientDemo{protectedintport=110;protectedStringhostname="localhost";protectedStringusername="";protectedStringpassword="";protectedSocketsocket;protectedBufferedReaderbr;protectedPrintWriterpw;//ConstructsanewinstanceofthePOP3clientpublicPop3ClientDemo()throwsException{try{//GetuserinputgetInput();//GetmailmessagesdisplayEmails();}catch(Exceptione){System.err.println("Erroroccured-detailsfollow");e.printStackTrace();System.out.println(e.getMessage());}}//ReturnsTRUEifPOPresponseindicatessuccess,FALSEiffailureprotectedbooleanresponseIsOk()throwsException{Stringline=br.readLine();System.out.println("<"+line);//和SMTP不同的地方,POP3的回覆不再是一个number而是//+OK来代 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 要求成功。失败则以-ERR来代表。returnline.toUpperCase().startsWith("+OK");}//ReadsalinefromthePOPserver,anddisplaysittoscreenprotectedStringreadLine(booleandebug)throwsException{Stringline=br.readLine();//Appenda"+msg);}//Closeallwriters,streamsandsocketsprotectedvoidcloseConnection()throwsException{pw.flush();pw.close();br.close();socket.close();}//SendtheQUITcommand,andcloseconnectionprotectedvoidsendQuit()throwsException{System.out.println("SendingQUIT");writeMsg("QUIT");readLine(true);System.out.println("ClosingConnection");closeConnection();}//DisplayemailsinamessageprotectedvoiddisplayEmails()throwsException{BufferedReaderuserinput=newBufferedReader(newInputStreamReader(System.in));System.out.println("Displayingmailboxwithprotocolcommands"+"andresponsesbelow");System.out.println("-----------------------------------------"+"---------------------");//OpenaconnectiontoPOP3serverSystem.out.println("OpeningSocket");socket=newSocket(this.hostname,this.port);br=newBufferedReader(newInputStreamReader(socket.getInputStream()));pw=newPrintWriter(newOutputStreamWriter(socket.getOutputStream()));//Ifresponsefromserverisnotokayif(!responseIsOk()){socket.close();thrownewException("InvalidPOP3Server");}//LoginbysendingUSERandPASScommandsSystem.out.println("Sendingusername");writeMsg("USER"+this.username);if(!responseIsOk()){sendQuit();thrownewException("Invalidusername");}System.out.println("Sendingpassword");writeMsg("PASS"+this.password);if(!responseIsOk()){sendQuit();thrownewException("Invalidpassword");}//Getmailcountfromserver....System.out.println("Checkingmail");writeMsg("STAT");//...andparsefornumberofmessagesStringline=readLine(true);StringTokenizertokens=newStringTokenizer(line,"");//+OKtokens.nextToken();//numberofmessagesintmessages=Integer.parseInt(tokens.nextToken());//sizeofallmessagesintmaxsize=Integer.parseInt(tokens.nextToken());if(messages==0){System.out.println("Therearenomessages.");sendQuit();return;}System.out.println("Thereare"+messages+"messages.");System.out.println("Pressentertocontinue.");userinput.readLine();for(inti=1;i<=messages;i++){System.out.println("Retrievingmessagenumber"+i);writeMsg("RETR"+i);System.out.println("--------------------");line=readLine(false);while(line!=null&&!line.equals(".")){line=readLine(false);}System.out.println("--------------------");System.out.println("Pressentertocontinue.Tostop,"+"typeQthenenter");Stringresponse=userinput.readLine();if(response.toUpperCase().startsWith("Q"))break;}sendQuit();}publicstaticvoidmain(String[]args)throwsException{Pop3ClientDemoclient=newPop3ClientDemo();}//ReaduserinputprotectedvoidgetInput()throwsException{Stringdata=null;BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));System.out.print("PleaseenterPOP3serverhostname:");data=br.readLine();if(data==null||data.equals(""))hostname="localhost";elsehostname=data;System.out.print("Pleaseentermailboxusername:");data=br.readLine();if(!(data==null||data.equals("")))username=data;System.out.print("Pleaseentermailboxpassword:");data=br.readLine();if(!(data==null||data.equals("")))password=data;}}【实验结果与分析】Ethereal抓包分析用Ethereal软件截获上面两个程序运行时客户机和服务器之间发送的数据包,并且根据截获的数据包内容进行分析。【实验结果与分析】host10.66.19.27tcpport25SMTP数据包结构下面的 表格 关于规范使用各类表格的通知入职表格免费下载关于主播时间做一个表格详细英语字母大小写表格下载简历表格模板下载 的值取自图中选中的数据包:前32位长度16位16位字段SourceportDestinationport值358825第2、3个32位:长度32位32位字段SequencenumberAcknowledgementnumber值82126下一个16位:1b(1位)长度4位6位1位1位1位1位1位1位字段HeaderlengthReservedURGACKPSHRSTSYNFIN值200011000再下一个32位:长度16位16位字段WindowsizeChecksum值654100x4336host10.66.19.27tcpport110Pop3数据包结构下面的表格的值取自图中选中的数据包:前32位长度16位16位字段SourceportDestinationport值3629110第2、3个32位:长度32位32位字段SequencenumberAcknowledgementnumber值166下一个16位:1b(1位)长度4位6位1位1位1位1位1位1位字段HeaderlengthReservedURGACKPSHRSTSYNFIN值200011000再下一个32位:长度16位16位字段WindowsizeChecksum值654700x2a01三.讨论、 心得 信息技术培训心得 下载关于七一讲话心得体会关于国企改革心得体会关于使用希沃白板的心得体会国培计划培训心得体会 记录实验感受、上机过程中遇到的困难及解决办法、遗留的问题、意见和建议等。
本文档为【实验七应用层网络编程一】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
雪桂
教书育人
格式:doc
大小:522KB
软件:Word
页数:0
分类:企业经营
上传时间:2021-10-20
浏览量:1