浙江大学城市学院实验报告课程名称计算机网络应用实验项目名称实验七应用层网络编程(一)实验成绩指导老师(签名)日期 2014-06-03 一. 实验目的和要求1.通过实现使用 Java 应用层客户端和服务器来获得关于使用Java Socket 网络编程的经验( SMTP、POP3)。二. 实验内容、原理及实验结果与分析1. SMTP 编程(参考电子讲义“网络编程参考资料-应用层 .pdf ”及教材“第2 章Socket 编程”)阅 读“ 网络 编 程参考 资 料 - 应 用层 .pdf ”“ SMTP客 户机 实 现” 的 源 代 码(SMTPClientDemo.java ),并在机器上编译运行通过。 (注:可输入城院 SMTP 邮件服务器或其他邮件服务器作为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 = "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) { } } 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(); 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(); } // Close all readers, streams and sockets protected void closeConnection() throws Exception { pw.flush(); pw.close(); br.close(); socket.close(); } ...