



版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、浙江大學城市學院實驗報告課程名稱實驗項目名稱實驗成績實驗七指導老師(簽名)計算機網(wǎng)絡應用應用層網(wǎng)絡編程(一)日期2014-06-03一 . 實驗目的和要求1.通過實現(xiàn)使用 Java 應用層客戶端和服務器來獲得關(guān)于使用 Java Socket 網(wǎng)絡編程的經(jīng)驗( SMTP、POP3)。二 . 實驗內(nèi)容、原理及實驗結(jié)果與分析1. SMTP 編程(參考電子講義 “網(wǎng)絡編程參考資料 -應用層 .pdf ”及教材“第2 章 Socket 編程”)閱讀 “網(wǎng)絡編程參考資料 -應用層 .pdf ”中 8.3.1 部分,實現(xiàn)“ SMTP 客戶機實現(xiàn)”的源代碼( SMTPClientDemo.java ),并在機
2、器上編譯運行通過。(注:可輸入城院 SMTP 郵件服務器 或其他郵件服務器作為 SMTP 服務器)【程序源代碼】SMTPClientDemo.javaimport java.io.*;import .*;import java.util.*;/ Chapter 8, Listing 1public class SMTPClientDemo protected int port = 25;protected String hostname = localhost;protected String from = ;protected S
3、tring to = ;protected String subject = ;protected String body = ;protected Socket socket;protected BufferedReader br;protected PrintWriter pw;/ Constructs a new instance of the SMTP Clientpublic SMTPClientDemo() throws Exception try getInput();sendEmail(); catch (Exception e) System.out.println (Err
4、or 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 = br.readLin
5、e(); System.out.println( +msg);/ Close all readers, streams and socketsprotected 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(Sending QU
6、IT);writeMsg(QUIT);readResponseCode();System.out.println(Closing Connection);closeConnection();/ Send an email message via SMTP , adhering to the protocol known as RFC 2821protected void sendEmail() throws Exception System.out.println(Sending message now: Debug below);System.out.println(- +-);System
7、.out.println(Opening Socket);socket = new Socket(this.hostname,this.port);System.out.println(Creating Reader & Writer);br = new BufferedReader(new InputStreamReader(socket.getInputStream();pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream();System.out.println(Reading first line);int
8、 code = readResponseCode();if(code != 220) socket.close();throw new Exception(Invalid SMTP Server);System.out.println(Sending helo command);writeMsg(HELO +InetAddress.getLocalHost().getHostName(); code = readResponseCode();if(code != 250) sendQuit();throw new Exception(Invalid SMTP Server);System.ou
9、t.println(Sending mail from command);writeMsg(MAIL FROM:);code = readResponseCode();if(code != 250) sendQuit();throw new Exception(Invalid from address);System.out.println(Sending rcpt to command);writeMsg(RCPT TO:);code = readResponseCode();if(code != 250) sendQuit();throw new Exception(Invalid to
10、address);System.out.println(Sending data command);writeMsg(DATA);code = readResponseCode();if(code != 354) sendQuit();throw new Exception(Data entry not accepted);System.out.println(Sending message);writeMsg(Subject: +this.subject);writeMsg(To: +this.to);writeMsg(From: +this.from);writeMsg();writeMs
11、g(body);code = readResponseCode();sendQuit();if(code != 250)throw new Exception(Message may not have been sent correctly);elseSystem.out.println(Message sent);/ Obtain input from the userprotected void getInput() throws Exception / Read input from user console String data=null;BufferedReaderbr=newBu
12、fferedReader(newInputStreamReader(System.in);/ Request hostname for SMTP server System.out.print(Please enter SMTP server hostname: ); data = br.readLine();if (data = null | data.equals() hostname=localhost;else hostname=data;/ Request the senders email address System.out.print(Please enter FROM ema
13、il address: ); data = br.readLine();from = data;/ Request the recipients email address System.out.print(Please enter TO email address :); data = br.readLine();if(!(data = null | data.equals() to=data;System.out.print(Please enter subject: );data = br.readLine();subject=data;System.out.println(Please
14、 enter plain-text message (. character + on a blank line signals end of message):);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.a
15、ppend(line);buffer.append(n); line = br.readLine();buffer.append(.n);body = buffer.toString();【實驗結(jié)果與分析】2. POP3 編程(參考電子講義 “網(wǎng)絡編程參考資料 -應用層 .pdf ”及教材“第2 章 Socket編程”)閱讀 “網(wǎng)絡編程參考資料 -應用層 .pdf ”中 8.3.2 部分,實現(xiàn)“ POP3 客戶實現(xiàn)”的源代碼( Pop3ClientDemo.java ),并在機器上編譯運行通過。(注:可輸入城院 POP3 郵件服務器 或其他郵件服務
16、器作為 POP3 服務器)【程序源代碼】Pop3ClientDemo.javaimport java.io.*;import .*;import java.util.*;public class Pop3ClientDemo protected int port = 110;protected String hostname = localhost;protected String username = ;protected String password = ;protected Socket socket;protected BufferedReader br;protec
17、ted PrintWriter pw;/ Constructs a new instance of the POP3 clientpublic Pop3ClientDemo() throws Exception try / Get user input getInput();/ Get mail messages displayEmails(); catch(Exception e) System.err.println (Error occured - details follow);e.printStackTrace();System.out.println(e.getMessage();
18、/ Returns TRUE if POP response indicates success, FALSE if failure protected boolean responseIsOk() throws Exception String line = br.readLine(); System.out.println( +line);/ 和 SMTP 不同的地方, POP3的回覆不再是一個number而是/ +OK 來代表要求成功。失敗則以 -ERR 來代表。 return line.toUpperCase().startsWith(+OK);/ Reads a line from
19、the POP server, and displays it to screen protected String readLine(boolean debug) throws Exception String line = br.readLine();/ Append a character to indicate this is a server protocol responseif (debug)System.out.println( +msg);/ Close all writers, streams and socketsprotected void closeConnectio
20、n() throws Exception pw.flush();pw.close();br.close();socket.close();/ Send the QUIT command, and close connection protected void sendQuit() throws Exception System.out.println(Sending QUIT);writeMsg(QUIT);readLine(true);System.out.println(Closing Connection);closeConnection();/ Display emails in a
21、messageprotected void displayEmails() throws Exception BufferedReader userinput = new BufferedReader( newInputStreamReader(System.in) );System.out.println(Displaying mailbox with protocol commands +and responses below);System.out.println(- +-);/ Open a connection to POP3 server System.out.println(Op
22、ening Socket); socket = new Socket(this.hostname, this.port);br = new BufferedReader(new InputStreamReader(socket.getInputStream();pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream();/ If response from server is not okayif(! responseIsOk() socket.close();throw new Exception(Invalid
23、POP3 Server);/ Login by sending USER and PASS commandsSystem.out.println(Sending username);writeMsg(USER +this.username);if(!responseIsOk() sendQuit();throw new Exception(Invalid username);System.out.println(Sending password);writeMsg(PASS +this.password);if(!responseIsOk() sendQuit();throw new Exce
24、ption(Invalid password);/ Get mail count from server .System.out.println(Checking mail); writeMsg(STAT);/ . and parse for number of messages String line = readLine(true);StringTokenizer tokens = new StringTokenizer(line, );/ +OKtokens.nextToken();/ number of messagesint messages = Integer.parseInt(t
25、okens.nextToken();/ size of all messagesint maxsize = Integer.parseInt(tokens.nextToken();if (messages = 0) System.out.println (There are no messages.);sendQuit();return;System.out.println (There are + messages + messages.); System.out.println(Press enter to continue.); userinput.readLine();for(int
26、i = 1; i = messages ; i+) System.out.println(Retrieving message number +i);writeMsg(RETR +i);System.out.println(-);line = readLine(false);while(line != null & !line.equals(.) line = readLine(false);System.out.println(-);System.out.println(Press enter to continue. To stop, + type Q then enter);String
27、 response = userinput.readLine();if (response.toUpperCase().startsWith(Q)break;sendQuit();public static void main(String args) throws Exception Pop3ClientDemo client = new Pop3ClientDemo();/ Read user inputprotected void getInput() throws Exception String data=null;BufferedReader br = new BufferedReader(new InputStreamReader(System.in);System.out.print(Please enter POP3 server hostname:); data = br.readLine();if(data = null | data.equals() hostname=localhost; else hostname=data;System.out.print(Please enter mailbox username:); data = br.readLine();if(!(dat
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國桂圓干行業(yè)競爭格局分析及投資戰(zhàn)略咨詢報告
- 2025年度車隊運輸節(jié)能減排服務協(xié)議
- 種樹合作合同范本
- 2025分公司承包旅游服務經(jīng)營合同范本
- 2025年度行政合同績效考核與激勵措施(第十二講)
- 《感受生活中的法律》(第一課時) 教學設(shè)計-2024-2025學年道德與法治六年級上冊統(tǒng)編版
- 中國裝修電商行業(yè)市場調(diào)查研究及投資前景預測報告
- 幾和第幾(教學設(shè)計)-2024-2025學年一年級上冊數(shù)學蘇教版
- 2025年有光外墻涂料項目可行性研究報告
- 2025年度職工宿舍建筑工程質(zhì)量檢測合同
- 網(wǎng)絡營銷講義網(wǎng)絡營銷產(chǎn)品策略課件
- 《小型混凝土預制件標準化生產(chǎn)管理辦法》
- 六年級上冊英語教案-Culture 2 Going Green 第二課時 廣東開心英語
- 警察叔叔是怎樣破案的演示文稿課件
- 青年教師個人成長檔案
- 2021譯林版高中英語選擇性必修三課文翻譯
- 2022年華中科技大學博士研究生英語入學考試真題
- 《網(wǎng)店運營與管理》整本書電子教案全套教學教案
- 打印版 《固體物理教程》課后答案王矜奉
- 中考《紅星照耀中國》各篇章練習題及答案(1-12)
- Q∕GDW 11612.43-2018 低壓電力線高速載波通信互聯(lián)互通技術(shù)規(guī)范 第4-3部分:應用層通信協(xié)議
評論
0/150
提交評論