实验七 应用层网络编程 下载本文

内容发布更新时间 : 2024/12/26 13:37:57星期一 下面是文章的全部内容请认真阅读。

浙江大学城市学院实验报告

课程名称 计算机网络应用 实验项目名称 实验七 应用层网络编程(一) 实验成绩 指导老师(签名) 日期 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 = \ 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(); }

// Send the QUIT protocol message, and terminate connection protected void sendQuit() throws Exception { writeMsg(\ readResponseCode(); closeConnection(); }

// Send an email message via SMTP, adhering to the protocol known as RFC 2821

protected void sendEmail() throws Exception { \ socket = new Socket(this.hostname,this.port); br = new BufferedReader(new InputStreamReader(socket.getInputStream())); pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); int code = readResponseCode(); if(code != 220) { socket.close();

throw new Exception(\ }

writeMsg(\ code = readResponseCode(); if(code != 250) {

sendQuit();

throw new Exception(\ }

writeMsg(\ code = readResponseCode(); if(code != 250) { sendQuit();

throw new Exception(\ }

writeMsg(\ code = readResponseCode(); if(code != 250) { sendQuit();

throw new Exception(\ }

writeMsg(\

code = readResponseCode(); if(code != 354) { sendQuit();

throw new Exception(\ }

writeMsg(\ writeMsg(\

writeMsg(\ writeMsg(\ writeMsg(body);

code = readResponseCode(); sendQuit(); if(code != 250)

throw new Exception(\ else }

// 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 data = br.readLine();

if (data == null || data.equals(\ else hostname=data;

// Request the sender's email address data = br.readLine(); from = data;