文档介绍:该【实验七应用层网络编程(1) 】是由【海洋里徜徉知识】上传分享,文档一共【14】页,该文档可以免费在线阅读,需要了解更多关于【实验七应用层网络编程(1) 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。 : .
浙江大学城市学院实验报告
课程名称 计算机网络应用
实验项目名称 实验七 应用层网络编程(一)
实验成绩 指导老师(签名) 日期 2014-06-03
一 . 实验目的和要求
1. 通过实现使用 Java 应用层客户端和服务器来获得关于使用 Java Socket
网络编程的经验( SMTP、POP3)。
二 . 实验内容、原理及实验结果与分析
1. SMTP 编程(参考电子讲义 “网络编程参考资料 -应用层 .pdf ”及教材“第
2 章 Socket 编程”)
阅读 “网络编程参考资料 -应用层 .pdf”中 部分,实现“ SMTP 客户
机实现”的源代码( ),并在机器上编译运行通过。( 注:
可输入城院 SMTP 邮件服务器 或其他邮件服务器作
为 SMTP 服务器)
【程序源代码】
import .*;
import .*;
import .*;
// Chapter 8, Listing 1
public class SMTPClientDemo {
protected int port = 25;
protected String hostname = "localhost";
protected String from = "";
protected String to = "";
protected String subject = "";
protected String body = "";
protected Socket socket;
protected BufferedReader br; : .
protected PrintWriter pw;
// Constructs a new instance of the SMTP Client
public SMTPClientDemo() throws Exception {
try {
getInput();
sendEmail();
} catch (Exception e) {
("Error sending message - " + e);
}
}
public static void main(String[] args) throws Exception {
// Start the SMTP client, so it can send messages
SMTPClientDemo client = new SMTPClientDemo();
}
// Check the SMTP response code for an error message
protected int readResponseCode() throws Exception {
String line = ();
("< "+line);
line = (0,(" "));
return (line);
}
// Write a protocol message both to the network socket and to the
screen
protected void writeMsg(String msg) throws Exception {
(msg);
();
("> "+msg);
}
// Close all readers, streams and sockets
protected void closeConnection() throws Exception {
();
();
();
();
}
// Send the QUIT protocol message, and terminate connection
protected void sendQuit() throws Exception {
("Sending QUIT"); : .
writeMsg("QUIT");
readResponseCode();
("Closing Connection");
closeConnection();
}
// Send an email message via SMTP , adhering to the protocol known as
RFC 2821
protected void sendEmail() throws Exception {
("Sending message now: Debug below");
("---------------------------------" +
"-----------------------------");
("Opening Socket");
socket = new Socket(,);
("Creating Reader & Writer");
br = new BufferedReader(new
InputStreamReader(()));
pw = new PrintWriter(new
OutputStreamWriter(()));
("Reading first line");
int code = readResponseCode();
if(code != 220) {
();
throw new Exception("Invalid SMTP Server");
}
("Sending helo command");
writeMsg("HELO "+().getHostName());
code = readResponseCode();
if(code != 250) {
sendQuit();
throw new Exception("Invalid SMTP Server");
}
("Sending mail from command");
writeMsg("MAIL FROM:<"++">");
code = readResponseCode();
if(code != 250) {
sendQuit();
throw new Exception("Invalid from address"); : .
}
("Sending rcpt to command");
writeMsg("RCPT TO:<"++">");
code = readResponseCode();
if(code != 250) {
sendQuit();
throw new Exception("Invalid to address");
}
("Sending data command");
writeMsg("DATA");
code = readResponseCode();
if(code != 354) {
sendQuit();
throw new Exception("Data entry not accepted");
}
("Sending message");
writeMsg("Subject: "+);
writeMsg("To: "+);
writeMsg("From: "+);
writeMsg("");
writeMsg(body);
code = readResponseCode();
sendQuit();
if(code != 250)
throw new Exception("Message may not have been sent
correctly");
else