浙江大学城市学院实验报告
课程名称 计算机网络应用 实验项目名称 实验七 应用层网络编程(一) 实验成绩 指导老师(签名) 日期 2014-06-03
一. 实验目的和要求
1. 通过实现使用Java应用层客户端和服务器来获得关于使用Java Socket
网络编程的经验(SMTP、POP3)。
二. 实验内容、原理及实验结果与分析
1. SMTP编程(参考电子讲义“网络编程参考资料-应用层.pdf”及教材“第
2章 Socket编程”)
阅读 “网络编程参考资料-应用层.pdf”中 8.3.1部分,实现“SMTP客户机实现”的源代码(SMTPClientDemo.java),并在机器上编译运行通过。(注:可输入城院SMTP邮件服务器smtp.email.zucc.edu.cn或其他邮件服务器作为SMTP服务器) 【程序源代码】
SMTPClientDemo.java import java.io.*; import java.net.*; import java.util.*;
// Chapter 8, Listing 1
public class SMTPClientDemo { protected int port = 25;
protected String hostname = \ 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) {
System.out.println (\ } }
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 = br.readLine(); System.out.println(\
line = line.substring(0,line.indexOf(\ return Integer.parseInt(line); }
// Write a protocol message both to the network socket and to the screen
protected void writeMsg(String msg) throws Exception { pw.println(msg); pw.flush();
System.out.println(\ }
// Close all readers, streams and sockets
protected void closeConnection() throws Exception { pw.flush(); pw.close(); br.close();
socket.close(); }
// Send the QUIT protocol message, and terminate connection protected void sendQuit() throws Exception { System.out.println(\
writeMsg(\ readResponseCode();
System.out.println(\ closeConnection(); }
// Send an email message via SMTP, adhering to the protocol known as RFC 2821
protected void sendEmail() throws Exception {
System.out.println(\ System.out.println(\ \
System.out.println(\
socket = new Socket(this.hostname,this.port);
System.out.println(\ br = new BufferedReader(new InputStreamReader(socket.getInputStream())); pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println(\ int code = readResponseCode(); if(code != 220) { socket.close();
throw new Exception(\ }
System.out.println(\
writeMsg(\ code = readResponseCode(); if(code != 250) { sendQuit();
throw new Exception(\ }
System.out.println(\ writeMsg(\ code = readResponseCode(); if(code != 250) { sendQuit();
throw new Exception(\
}
System.out.println(\ writeMsg(\ code = readResponseCode(); if(code != 250) { sendQuit();
throw new Exception(\ }
System.out.println(\ writeMsg(\
code = readResponseCode(); if(code != 354) { sendQuit();
throw new Exception(\ }
System.out.println(\ writeMsg(\ writeMsg(\
writeMsg(\ writeMsg(\ writeMsg(body);
code = readResponseCode(); sendQuit(); if(code != 250)
throw new Exception(\may not have been sent correctly\ else
System.out.println(\ }
// Obtain input from the user
protected void getInput() throws Exception { // Read input from user console String data=null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Request hostname for SMTP server
System.out.print(\ data = br.readLine();
if (data == null || data.equals(\
else hostname=data;
// Request the sender's email address
System.out.print(\ data = br.readLine(); from = data;
// Request the recipient's email address
System.out.print(\ data = br.readLine();
if(!(data == null || data.equals(\ to=data;
System.out.print(\ data = br.readLine(); subject=data;
System.out.println(\ \ StringBuffer buffer = new StringBuffer();
// Read until user enters a . on a blank line String line = br.readLine(); while(line != null) {
// Check for a '.', and only a '.', on a line if(line.equalsIgnoreCase(\ break; }
buffer.append(line); buffer.append(\ line = br.readLine(); }
buffer.append(\ body = buffer.toString(); } }
【实验结果与分析】