欢迎来到天天文库
浏览记录
ID:22668914
大小:518.04 KB
页数:15页
时间:2018-10-30
《实验七应用层网络编程(一)》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、浙江大学城市学院实验报告课程名称_实验项目名称实验成绩计算机网络应实验七应用层网络编程(一)指导老师(签名)闩期2014-06-03一.实验0的和要求1.通过实现使用Java应用层客户端和服务器来获得关于使用JavaSocket网络编程的经验(SMTP、POP3)。二.实验内容、原理及实验结果与分析1-SMTP编程(参考电子讲义“网络编程参考资料•应用层.pdf”及教材“第2章Socket编程”)阅读“网络编程参考资料-应用层.pdf”中8.3.1部分,实现“SMTP客户机实现”的源代码(SMTPCIientDemo.java),并在机器上编译运行通过。(注:可输入城院
2、SMTP邮件服务器smtp.email.zucc.edu.cn或其他邮件服务器作为SMTP服务器)【程序源代码】SMTPCIientDemo.javaimportjava.io.*;importjava.net/;importjava.util.*;//Chapter8,Listing1publicclassSMTPCIientDemo{protectedintport=25;protectedStringhostname="localhost";protectedStringfrom=…’;protectedStringto="”;protectedStringsub
3、ject="”;protectedStringbody=protectedSocketsocket;protectedBufferedReaderbr;protectedPrintWriterpw;//ConstructsanewinstanceoftheSMTPClientpublicSMTPCIientDemo()throwsException{try{getlnput();sendEmail();}catch(Exceptione){System.out.println("Errorsendingmessage-”+e);}}publicstaticvoidmai
4、n(String[]args)throwsException{//StarttheSMTPclient,soitcansendmessagesSMTPCIientDemoclient=newSMTPCIientDemo();}//ChecktheSMTPresponsecodeforanerrormessageprotectedintreadResponseCode()throwsException{Stringline=br.readLine();System.out.println("<"+line);line=line.substring(O,line.index
5、Of(M**));returnInteger.parselnt(line);}//WriteaprotocolmessagebothtothenetworksocketandtothescreenprotectedvoidwriteMsg(Stringmsg)throwsException{pw.println(msg);pw.flush();System.out.println(”〉"+msg);}//Closeallreaders,streamsandsocketsprotectedvoidcloseConnection()throwsException{pw.fl
6、ush();pw.close();br.close();socket.close();}//SendtheQUITprotocolmessage,andterminateconnectionprotectedvoidsendQuit()throwsException{System.out.println(”SendingQUIT");writeMsg("QUir’);readResponseCode();System.out.println("ClosingConnection");closeConnection();//SendanemailmessageviaSMT
7、P,adheringtotheprotocolknownasRFC2821protectedvoidsendEmail()throwsException{System.out.printlnfSendingmessagenow:Debugbelow");System.out.println(•’"+System.out.printlnf’OpeningSocket");socket=newSocket(this.hostname,this.port);System.out.println(HCreatingReader&Writer");
此文档下载收益归作者所有