版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、浙江大學(xué)城市學(xué)院實(shí)驗(yàn)報(bào)告課程名稱實(shí)驗(yàn)項(xiàng)目名稱實(shí)驗(yàn)成績實(shí)驗(yàn)七指導(dǎo)老師(簽名)計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用應(yīng)用層網(wǎng)絡(luò)編程(一)日期2014-06-03一 . 實(shí)驗(yàn)?zāi)康暮鸵?.通過實(shí)現(xiàn)使用 Java 應(yīng)用層客戶端和服務(wù)器來獲得關(guān)于使用 Java Socket 網(wǎng)絡(luò)編程的經(jīng)驗(yàn)( SMTP、POP3)。二 . 實(shí)驗(yàn)內(nèi)容、原理及實(shí)驗(yàn)結(jié)果與分析1. SMTP 編程(參考電子講義 “網(wǎng)絡(luò)編程參考資料 -應(yīng)用層 .pdf ”及教材“第2 章 Socket 編程”)閱讀 “網(wǎng)絡(luò)編程參考資料 -應(yīng)用層 .pdf ”中 8.3.1 部分,實(shí)現(xiàn)“ SMTP 客戶機(jī)實(shí)現(xiàn)”的源代碼( SMTPClientDemo.java ),并在機(jī)
2、器上編譯運(yùn)行通過。(注:可輸入城院 SMTP 郵件服務(wù)器 或其他郵件服務(wù)器作為 SMTP 服務(wù)器)【程序源代碼】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();【實(shí)驗(yàn)結(jié)果與分析】2. POP3 編程(參考電子講義 “網(wǎng)絡(luò)編程參考資料 -應(yīng)用層 .pdf ”及教材“第2 章 Socket編程”)閱讀 “網(wǎng)絡(luò)編程參考資料 -應(yīng)用層 .pdf ”中 8.3.2 部分,實(shí)現(xiàn)“ POP3 客戶實(shí)現(xiàn)”的源代碼( Pop3ClientDemo.java ),并在機(jī)器上編譯運(yùn)行通過。(注:可輸入城院 POP3 郵件服務(wù)器 或其他郵件服務(wù)
16、器作為 POP3 服務(wù)器)【程序源代碼】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的回覆不再是一個(gè)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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度科技賦能小區(qū)綠化施工合同3篇
- 二零二五年度勞動(dòng)法適用下的企業(yè)員工加班費(fèi)合同3篇
- 二零二五年度城市綠地使用權(quán)購買合同3篇
- 二零二五年度房地產(chǎn)公司財(cái)務(wù)風(fēng)險(xiǎn)管理體系建設(shè)合同3篇
- 二零二五年度商用電器銷售與租賃合同3篇
- 二零二五年度企業(yè)上市與股權(quán)轉(zhuǎn)讓合同3篇
- 海南體育職業(yè)技術(shù)學(xué)院《家具設(shè)計(jì)》2023-2024學(xué)年第一學(xué)期期末試卷
- 二零二五年度新能源儲(chǔ)能系統(tǒng)安裝與運(yùn)營總承包服務(wù)協(xié)議3篇
- 2025年度網(wǎng)絡(luò)游戲運(yùn)營與代理合同3篇
- 二零二五年度城市廣場附屬設(shè)施承包合同6篇
- 2024-2030年中國Micro LED行業(yè)發(fā)展現(xiàn)狀調(diào)研及市場前景趨勢報(bào)告
- 2024年全國職業(yè)院校技能大賽“新型電力系統(tǒng)與維護(hù)”賽項(xiàng)考試題庫-中(多選題)
- 除濕機(jī)濕度調(diào)節(jié)能力考核試卷
- 朗誦社團(tuán)活動(dòng)教案
- 宜賓市翠屏區(qū)2022-2023學(xué)年七年級(jí)上學(xué)期期末地理試題
- 汽車智能座艙交互體驗(yàn)測試評(píng)價(jià)規(guī)程
- 上海中考考綱詞匯默寫每天50個(gè)(無答案)
- 腔鏡右半結(jié)腸手術(shù)配合
- 十八項(xiàng)醫(yī)療核心制度培訓(xùn)課件
- 大型集團(tuán)公司內(nèi)部控制固定資產(chǎn)折舊制度
- 工地食堂經(jīng)營方案及計(jì)劃書
評(píng)論
0/150
提交評(píng)論