77范文网 - 专业文章范例文档资料分享平台

实验七 应用层网络编程(一)

来源:网络收集 时间:2020-04-14 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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

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

【实验结果与分析】

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库实验七 应用层网络编程(一)在线全文阅读。

实验七 应用层网络编程(一).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/jiaoyu/938782.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: